【问题标题】:Spring AOP apply to all methods that are not finalSpring AOP 适用于所有非 final 的方法
【发布时间】:2019-04-07 03:02:27
【问题描述】:

我有以下方面...

@Aspect
@Component
public class RestrictAspect {

    protected static final Logger logger = LoggerFactory.getLogger(RestrictAspect.class);

    @Pointcut(value = "execution(public * *(..))")
    public void allMethods() {
    }

    @Around("allMethods() && @annotation(restrict)")
    public Object checkAuthorization(ProceedingJoinPoint pjp, Restrict restrict) throws Throwable {
        if (pjp != null && restrict != null && restrict.toRole() >= 0) {

            // get the session info -> then the users
            SessionInformation si = (SessionInformation) FacesContext.getCurrentInstance()
                    .getExternalContext()
                    .getSessionMap()
                    .get(XxxConstants.SESSION_SESSIONINFO);

            if (si == null || si.getUser() == null) {
                final String msg = "No authenticated user found.";
                logger.warn("Throwing InvalidAccessException: {}", msg);
                throw new InvalidAccessException(msg);
            }

            final User user = si.getUser();

            if (!user.isAccountAdmin()) {
                final String msg = "User is not an administrator.";
                logger.warn("Throwing InvalidAccessException: {}", msg);
                throw new InvalidAccessException(msg);
            }

            if (AdminUserRoles.hasRightsTo(user.getRole(), restrict.toRole())) {
                return pjp.proceed();
            } else {
                final String msg = "User does not have access to " + getRoleName(restrict.toRole(), new StringLanguage(MESSAGES, Locale.getDefault())) + "(" + restrict.toRole() + ")";
                logger.warn("Throwing InvalidAccessException: {}", msg);
                throw new InvalidAccessException(msg);
            }
        }
        final String msg = "Unable to grant access.";
        logger.warn("Throwing InvalidAccessException: {}", msg);
        throw new InvalidAccessException(msg);
    }
}

问题是它试图将方面应用到我的类中的最终方法,并抛出以下警告:

07 Apr 2019 02:47:11  INFO o.s.a.f.CglibAopProxy:266 [admin@test.com @ 1] - Final method [protected final javax.servlet.http.HttpServletRequest net.xxxx.beans.XxxBean.getHttpServletRequest()] cannot get proxied via CGLIB: Calls to this method will NOT be routed to the target instance and might lead to NPEs against uninitialized fields in the proxy instance.
07 Apr 2019 02:47:11  INFO o.s.a.f.CglibAopProxy:266 [admin@test.com @ 1] - Final method [public final java.lang.String net.xxxx.beans.XxxBean.getTimeZone()] cannot get proxied via CGLIB: Calls to this method will NOT be routed to the target instance and might lead to NPEs against uninitialized fields in the proxy instance.
07 Apr 2019 02:47:11  INFO o.s.a.f.CglibAopProxy:266 [admin@test.com @ 1] - Final method [protected final void net.zzz.beans.XxxBean.log(java.lang.String)] cannot get proxied via CGLIB: Calls to this method will NOT be routed to the target instance and might lead to NPEs against uninitialized fields in the proxy instance.

所有错误都是由于在应用 Aspect 的基类中定义的一系列最终方法造成的。有什么方法可以编写我的 PointCut / execution() 使其不尝试将其应用于最终方法以删除我的错误并清理我的日志?

谢谢!

【问题讨论】:

    标签: spring aop spring-aop


    【解决方案1】:

    您看到的不是错误,只是警告。尽管如此,正如您所建议的那样,更精确地使用您的切入点是有意义的:

    @Pointcut(value = "execution(public !final * *(..))")
    public void nonFinalPublicMethods() {}
    

    顺便说一句,如果你想从 Spring AOP 切换到 AspectJ,你也可以编织 final 方法,因为 AspectJ 不依赖于动态代理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      相关资源
      最近更新 更多