如何在EJB拦截器的生命周期事件方法中获取调用者名称

发布于 2024-12-18 13:03:22 字数 1757 浏览 3 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞄了个咪的 2024-12-25 13:03:22

对于生命周期回调,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?

塔塔猫 2024-12-25 13:03:22

在 WebLogic 服务器上,您可以在 postConstructor 等中使用它来获取 EJB 名称:

ctx.getTarget().getClass().getSuperclass().getName();

On WebLogic server you can use this in postConstructor, etc, to get the EJB name:

ctx.getTarget().getClass().getSuperclass().getName();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文