Spring AOP适用于所有非最终方法

时间:2019-04-07 03:02:28

标签: spring aop spring-aop

我有以下方面...

@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(),使其不尝试将其应用于最终方法以消除错误并清理日志?

谢谢!

1 个答案:

答案 0 :(得分:2)

您看到的不是错误,只是警告。仍然,按照您的建议,使用切入点更加精确是有意义的:

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

顺便说一句,如果您要从Spring AOP切换到AspectJ,您还可以编织最终方法,因为AspectJ不依赖于动态代理。