struts2 ActionInvocation 获取不到注解的值,麻烦各位帮忙看下?

注解

/**
 * 系统日志注解
 *
 * @author linyuting
 * @since 2018-3-13
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLogAnnotation {

    String value() default "";
}

拦截器

    @Override
    public String intercept(ActionInvocation ai)
        throws Exception
    {
        try
        {
            String methodName = ai.getProxy().getMethod();
            Method currentMethod = ai.getAction().getClass().getMethod(methodName, null);
            if (currentMethod.isAnnotationPresent(SysLogAnnotation.class))
            {
                SysLogAnnotation sysLogAnnotation = currentMethod.getAnnotation(SysLogAnnotation.class);
                if (sysLogAnnotation != null)
                {
                    StringUtil.log(sysLogAnnotation.value());
                }
            }

            StringUtil.log(StringUtil.log(new Date()));
            SysLogin user = (SysLogin)SecurityUtils.getSubject().getPrincipal();
            if (user != null)
            {
                StringUtil.log("操作人:" + user.getName());
            }
            else
            {
                StringUtil.log("操作人:系统未获取");
            }
            StringUtil.log("类名:" + ai.getAction() + " ");
            StringUtil.log("方法名:" + ai.getInvocationContext().getName() + " ");
            Map<String, Object> map = ai.getInvocationContext().getParameters();
            Set<String> keys = map.keySet();
            StringUtil.log("参数:");
            for (String key : keys)
            {
                StringUtil.log(key + "=" + ((Object[])map.get(key))[0] + "#");
            }
            StringUtil.log(" ");
            StringUtil.log("执行结果:" + ai.getResultCode() + " ");
            // SysLog syslog = new SysLog();
            // sysLogService.insert(syslog);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return ai.invoke();
    }
阅读 4.3k
1 个回答

这个是因为spring的动态代理,把class去掉动态代理那块字符串,再转成类就能获取到注解了.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题