【问题标题】:Get input value to ejb 3.1 throw interceptor获取ejb 3.1 throw拦截器的输入值
【发布时间】:2016-10-21 11:03:42
【问题描述】:

你知道有没有办法记录,抛出一个拦截器,被调用方法的输入值?

我的实际拦截器是

public class Interceptor {
@AroundInvoke
public Object interceptor(InvocationContext invocationcontext) throws Exception{
    //Stampa prima del metodo
    long startTime = System.currentTimeMillis();
    log.debug("Invoked method: "+invocationcontext.getMethod().getName());
    //here I would like to log also parameters. 
    try{
        return invocationcontext.proceed();
    } finally{
        log.debug("End of method: " + invocationcontext.getMethod().getName());
        log.debug(" duration: " + (System.currentTimeMillis() - startTime));
    }
}
}

豆子是

@Interceptors({Interceptor.class})

@无状态 公共类 MrBean 实现 MrBeanRemote, MrBeanLocal {

/**
 * Default constructor. 
 */
public MrBean() {       
}

public void print(String in){
    System.out.println("Print: " + in);
}
}

所以如果我用 in = "print that" 调用 print 方法,拦截器应该记录 "print that"。可能吗? 提前致谢

【问题讨论】:

    标签: java interceptor ejb-3.1


    【解决方案1】:

    您想记录方法的参数,因此您可以在 InvocationContext 上使用 getParameters() 方法: http://docs.oracle.com/javaee/6/api/javax/interceptor/InvocationContext.html#getParameters()

    【讨论】:

    • 我试过 log.debug(invocationcontext.getParameters().toString()); [Ljava.lang.Object;@f72802c4
    • 对不起,我无法编辑上面的评论...我想说我尝试了该日志,但它打印字符串 [Ljava.lang.Object;@f72802c4 而不是值的参数
    • @Gio : invocationcontext.getParameters() 返回对象数组。你需要遍历这个数组。
    • @Gio:Obscure 是对的,遍历值数组,你应该找到你的参数。 for (Object o : invocationcontext.getParameters()) { log.debug(o); }
    猜你喜欢
    • 1970-01-01
    • 2014-03-10
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 2014-09-12
    • 2017-03-06
    • 2021-12-18
    相关资源
    最近更新 更多