如何在EJB拦截器的生命周期事件方法中获取调用者名称
我使用 Java EE 5。我为所有 EJB 编写了一个拦截器,其中包含三种日志记录方法:
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");
}
}
}
我想获取调用此拦截器的 EJB 的名称。我该怎么做呢?
我尝试了 ctx.getMethod().getDeclaringClass().getSimpleName()
但 ctx.getMethod()
在 postConstruct() 中返回
和 null
-)predestroy(-)
方法。
I use Java EE 5. I wrote an interceptor for all EJBs with three methods for logging:
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");
}
}
}
I want to get the name of EJB which called this interceptor. How can I do it?
I tried ctx.getMethod().getDeclaringClass().getSimpleName()
but ctx.getMethod()
returns null
in postConstruct(-)
and predestroy(-)
methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于生命周期回调,ctx.getMethod() 返回 null。例如,此处记录了这一点:http://docs.oracle。 com/javaee/5/api/javax/interceptor/InitationContext.html
是这样,因为调用生命周期回调方法的不是您的 EJB,而是容器。
如果您对拦截器和它所属的 bean 之间的关联感兴趣,那么 ctx.getTarget() 方法不符合您的目的吗?
For lifecycle callbacks ctx.getMethod() returns null. This is documented for example here: http://docs.oracle.com/javaee/5/api/javax/interceptor/InvocationContext.html
That is so, because it is not your EJB, but container who calls lifecycle callback methods.
If you are interested about association between interceptor and bean it belongs to, doesn't ctx.getTarget() method serve your purpose?
在 WebLogic 服务器上,您可以在 postConstructor 等中使用它来获取 EJB 名称:
On WebLogic server you can use this in postConstructor, etc, to get the EJB name: