我在 pom.xml 中添加了以下依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
并在 appContext.xml 中启用 AspectJ,如下所示:
并定义aspect如下:
@Component
@Aspect
public class AuthenticationServiceAspect {
@Before("execution(* com.service.impl.AuthenticationServiceImpl.*(..))")
public void adviceMethod(JoinPoint joinPoint) {
if(true){
throw new Exception();
}
}
现在我想禁用这个 AOP,这样上面的代码就不会被执行?我该怎么做?
最佳答案
您可以使用带有 ConditionalOnExpression 注释的属性的启用/禁用组件。当组件被禁用时,方面也是如此。
@Component
@Aspect
@ConditionalOnExpression("${authentication.service.enabled:true}")// enabled by default
public class AuthenticationServiceAspect {
@Before("execution(* com.service.impl.AuthenticationServiceImpl.*(..))")
public void adviceMethod(JoinPoint joinPoint) {
if(true){
throw new Exception();
}
}
}
要禁用方面,只需将 authentication.service.enabled=false 添加到您的 application.properties。
关于java - 我们可以根据任何标志的值或通过配置文件启用或禁用方面吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29406192/