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

问题描述 投票:0回答:1

我有以下方面......

@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 [[email protected] @ 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 [[email protected] @ 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 [[email protected] @ 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个回答
2
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.