tôi sử dụng Java EE 5. Tôi đã viết một đánh chặn cho tất cả các EJB với ba phương pháp để khai thác gỗ:Làm thế nào để có được tên Invoker trong phương pháp kiện vòng đời EJB đánh chặn của
public class DefaultInterceptor {
public static final String PREFIX = "!!!!!!!!!Interceptor:";
@PostConstruct
public void postConstruct(InvocationContext ctx) {
try {
System.out.println(PREFIX + " postConstruct");
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@PreDestroy
public void preDestroy(InvocationContext ctx) {
try {
System.out.println(PREFIX + " predestroy");
System.out.println(PREFIX + "ctx.preceed=" + ctx.proceed());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
System.out.println(PREFIX + "method invocation '" + ctx.getMethod().getName() + "'");
System.out.println(PREFIX + "parameters ='" + Arrays.deepToString(ctx.getParameters()) + "'");
System.out.println(Arrays.deepToString(ctx.getContextData().keySet().toArray()));
Object result = null;
try {
result = ctx.proceed();
System.out.println(PREFIX + "Method result='" + result + "'");
return result;
} catch (Exception ex) {
System.out.println(PREFIX + "Method exception ='" + ex.getMessage() + "'");
throw ex;
} finally {
System.out.println(PREFIX + "Method finished");
}
}
}
Tôi muốn có được tên của EJB mà được gọi là interceptor này. Tôi làm nó như thế nào?
Tôi đã thử ctx.getMethod().getDeclaringClass().getSimpleName()
nhưng ctx.getMethod()
trả về null
trong các phương thức postConstruct(-)
và predestroy(-)
.
Lệnh 'ctx.getMethod()' trả về null hoặc 'ctx' ** được ** null? –
@Piotr Nowicki ctx.getMethod() trả về luôn null trong callbacks vòng đời. –
@MikkoMaunu yeah, tôi biết rằng nó được gọi bởi container, không phải là phương pháp invoker. Tôi đã quan tâm nhiều hơn rằng phương thức gọi lại vòng đời có thể lấy tham số 'InvocationContext', nhưng tôi đã quên rằng các Interceptor có thể :-) –