java 我们可以根据任何标志的值或通过配置文件启用或禁用 Aspect 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29406192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we enable or disable Aspect based on value of any flag or through configuration file?
提问by user2758176
I have added following dependency in pom.xml
我在 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>
And enable AspectJ in appContext.xml as follows:
并在 appContext.xml 中启用 AspectJ,如下所示:
And define aspect as follows:
并定义方面如下:
@Component
@Aspect
public class AuthenticationServiceAspect {
@Before("execution(* com.service.impl.AuthenticationServiceImpl.*(..))")
public void adviceMethod(JoinPoint joinPoint) {
if(true){
throw new Exception();
}
}
Now I want to disable this AOP so that above code won't get execute? How can I do this?
现在我想禁用这个 AOP,这样上面的代码就不会被执行?我怎样才能做到这一点?
回答by Eric Georges
You may use enabling/disabling component with properties with ConditionalOnExpression annotation. When component is disabled the aspect is too.
您可以使用具有 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();
}
}
}
To disabling the aspect, just add authentication.service.enabled=falseto your application.properties.
要禁用方面,只需将authentication.service.enabled=false添加到您的 application.properties。
回答by datree
It appears Spring does not support AspectJ's if()
pointcut primitive.
Spring 似乎不支持 AspectJ 的if()
切入点原语。
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'foo': Initialization of bean failed; nested exception is org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException: Pointcut expression 'execution(* doSomething(..)) && if()' contains unsupported pointcut primitive 'if'
引起:org.springframework.beans.factory.BeanCreationException:创建名为“foo”的bean时出错:bean初始化失败;嵌套异常是 org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException: 切入点表达式 'execution(* doSomething(..)) && if()' 包含不受支持的切入点原语 'if'
回答by sheltem
There is always the if() pointcut expression that allows you to define a condition to be checked by the advice: https://www.eclipse.org/aspectj/doc/next/adk15notebook/ataspectj-pcadvice.html#d0e3697
总是有 if() 切入点表达式允许您定义要通过建议检查的条件:https: //www.eclipse.org/aspectj/doc/next/adk15notebook/ataspectj-pcadvice.html#d0e3697
And... your aspect already is a Spring @Component. Why not just inject properties via @Value and decide on them whether your advice should execute? You can do that via the if() pointcut described above or by just checking the value in the advice and either executing or skipping because of it.
而且……您的方面已经是一个 Spring @Component。为什么不直接通过@Value 注入属性并决定是否应该执行您的建议?您可以通过上述 if() 切入点或仅检查通知中的值并因此执行或跳过来完成此操作。
@Component
@Aspect
public class AuthenticationServiceAspect {
@Value("${aspect.active}")
private boolean active = true;
@Pointcut("execution(* com.service.impl.AuthenticationServiceImpl.*(..)) && if()")
public static boolean conditionalPointcut() {
return active;
}
@Before("conditionalPointcut()")
public void adviceMethod(JoinPoint joinPoint) {
if(true){
throw new Exception();
}
}
}