AOP切入点仅适用于带注释的方法

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

我正在使用AspectJ和Spring AOP,但是我面临一个奇怪的问题,切入点仅适用于上面带有一些注释的那些方法,例如ovverride,Bean等。切入点不适用于以下方法:没有注释的类。

下面是我正在使用的配置:

@Aspect
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
//@EnableLoadTimeWeaving
public class AspectLogging {

    private static final Logger logger = LoggerFactory.getLogger(AspectLogging.class);




      @Pointcut("execution(public * *(..))")//Public 
      public void publicMethod(){};

      @Pointcut("execution(protected * *(..))")//Protected 
      public void protectedMethod(){}

      //@Pointcut("execution(* com.s4m.user.*.*(..))")
      @Pointcut("within(com.s4m.user..*)")
     // @Pointcut("@annotation(Service)")
      public void annotationPointcut(){}

      @Pointcut("execution(private * *(..))")//Protected 

      public void privateMethod(){}


    @Before("annotationPointcut() && (protectedMethod() || publicMethod()  || privateMethod())")
    public void test(JoinPoint joinpoint) {
          logger.info(joinpoint.getSourceLocation().getWithinType().getSimpleName() +" :: "+ joinpoint.getSignature().getName() + " **Entry**");
    }

}   

例如,下面是同一类的方法,切入点适用于带注释的方法,但不适用于其他方法。

@Override
    public Object logout(HttpServletRequest request) {
        loggingOut(request);
        return Utility.getResponseModel(ApiConstants.SUCCESS);
    }

    public void loggingOut(HttpServletRequest request) {
        HttpSession session = request.getSession();
        RedisUser redis = redisUserRepository.findById(request.getHeader(ApiConstants.DEVICE_ID));
        if (!Util.objectIsNull(redis)) {
            deleteUserInRedis(redis);
            saveAuditTrail(ApiConstants.LOGOUT, redis.getSessionId(), redis.getName(), redis.getDeviceId(),
                    ApiConstants.OPERATION_SUCCESSFUL, true);
        }
        session.invalidate();
    }

以上方法的日志:

AC66A549C3416D3 2019-10-02 15:51:08 [http-nio-8302-exec-2] INFO  com.s4m.user.config.AspectLogging -CITI-P_003(AD PLUGIN)- UserServiceImpl :: logout**Entry**

但是该方法没有日志,因为切入点不起作用:

public void loggingOutTest() {

    }
java spring-boot aop spring-aop spring-annotations
2个回答
0
投票

Spring AOP仅适用于Spring bean(也可以通过对类进行注释来创建Spring bean)。而且,您也只能拦截公共方法调用。这里仅支持AspectJ的有限功能。


0
投票

Spring AOP只能识别Spring bean。如果您想使用其他对象(例如JPA实体),请尝试添加AspectJ依赖项:https://mvnrepository.com/artifact/org.aspectj/aspectjrt

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