基于Spring AOP Schema执行了不正确的方面类

时间:2018-09-25 08:04:42

标签: java spring spring-boot aop spring-aop

// aspect-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean name="referenceAspect"
        class="com.example.reference.audit.aspect.ReferenceAuditAspect"></bean>
    <bean name="mainApplicationTypeAspect"
        class="com.example.maintenance.audit.aspect.MaintApplicationTypeAuditAspect"></bean>

    <aop:aspectj-autoproxy proxy-target-class="true" />
    <aop:config>
        <aop:aspect ref="referenceAspect">
            <!-- @around sysad log annotation -->
            <aop:pointcut id="referencePointCut" expression="@annotation(etisLog)" />
            <aop:around method="auditLogAround" pointcut-ref="referencePointCut" />
        </aop:aspect>

        <aop:aspect ref="mainApplicationTypeAspect">
            <!-- @around sysad log annotation -->
            <aop:pointcut id="mainApplicationTypePointCut" expression="@annotation(etisLog)" />
            <aop:around method="auditLogAround" pointcut-ref="mainApplicationTypePointCut" />
        </aop:aspect>

    </aop:config>

</beans>  

我有两个方面的类:

@Component
public class ReferenceAuditAspect {


  public Object auditLogAround(ProceedingJoinPoint joinPoint, ETISLog etisLog)
      throws Throwable {
    LOGGER.info("INTERCEPTING REFERENCE SERVICE...");
  }
}

@Component
public class MaintApplicationTypeAuditAspect  {


  public Object auditLogAround(ProceedingJoinPoint joinPoint, ETISLog etisLog)
      throws Throwable {
    LOGGER.info("INTERCEPTING REFERENCE SERVICE...");

  }

}

//我的切入点表达式

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ETISLog {

}

//在API级别进行拦截

@ETISLog
  public ResponseEntity<MaintApplicationType> save(
      @RequestBody MaintApplicationType maintApplicationType) {
    LOGGER.info("API: SAVE {}", maintApplicationType);

    return new ResponseEntity<>(maintApplicationTypeService.save(maintApplicationType),
        HttpStatus.CREATED);
  }

但是当我运行应用程序并在maintApplicationApi下执行保存操作时,为什么总是执行的方面是ReferenceAuditAspect而不是mainApplicationTypeAspect。我在auditLogAround内部有执行特定操作的代码。

我在这里遵循了文档:https://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/aop.html-> 8.3节

我也尝试删除:<aop:aspectj-autoproxy proxy-target-class="true" />但存在相同问题

1 个答案:

答案 0 :(得分:0)

我的解决方法是为每个方面创建一个新的切入点注释符号。