使用环境变量启用 aspectj
enabling aspectj with environment variables
我们如何enable/disable一个方面使用环境变量?
我知道可以 enable/disable 在 spring 启动应用程序中使用以下属性的 aspectj
spring:
aop:
auto: true
或者:
spring.aop.auto=true
并删除 @EnableAspectJAutoProxy
,但这会停止我们所有其他方面/连接。
这是我要禁用的,我该怎么做
@Aspect
@Component
public class SomeAspect {
@Around("@annotation(someAnnotation)")
public Object doSomething(ProceedingJoinPoint joinPoint, SomeAnnotation sa) throws Throwable {
// ...
}
//others
}
为了在方面 class 内动态停用单个建议,您可以使用 if()
切入点。
如果您想根据条件完全禁用某个方面(或任何其他 Spring bean 或组件),例如application.config
中的 属性,看看 @Conditional
及其特殊情况 @ConditionalOn*
。例如:
@Aspect
@Component
@ConditionalOnProperty(prefix = "org.acme.myapp", name = "aspect_active")
public class SomeAspect {
// ...
}
application.config
中的类似内容会停用方面:
org.acme.myapp.aspect_active=false
如果应用程序配置中根本没有这样的 属性,则该方面也将处于非活动状态。如果您更愿意默认为活动方面,只需使用
@ConditionalOnProperty(prefix = "org.acme.myapp", name = "aspect_active", matchIfMissing = true)
您可以按照 javadoc 中的描述进一步微调行为。
另请参阅:
- https://www.baeldung.com/spring-conditional-annotations
- https://www.baeldung.com/spring-conditionalonproperty
更新:
In order to dynamically deactivate a single advice inside an aspect class, you can use an if()
pointcut.
糟糕,抱歉,我是 AspectJ 的原生用户,忘记了 Spring AOP 不支持 if()
切入点指示符。所以你能做的最好的事情可能是在建议的开头使用 if
表达式,具体取决于 @Value
属性.
@Value("${org.acme.myapp.advice_active:false}")
private boolean adviceActive;
@Around("@annotation(someAnnotation)")
public Object doSomething(ProceedingJoinPoint joinPoint, SomeAnnotation sa) throws Throwable {
// Skip advice logic if inactive, simply proceed and return original result
if (!adviceActive)
return joinPoint.proceed();
// Regular advice logic if active
System.out.println(joinPoint);
// Either also proceed or do whatever else is the custom advice logic, e.g.
// - manipulate method arguments,
// - manipulate original return value,
// - skip proceeding to the original method altogether and return something else.
return joinPoint.proceed();
}
当然,如果您需要那种粒度,您也可以使用我的原始解决方案,将您希望停用的建议分解为一个单独的方面 class。这样麻烦会少一些,建议方法的代码也更易读。
我们如何enable/disable一个方面使用环境变量?
我知道可以 enable/disable 在 spring 启动应用程序中使用以下属性的 aspectj
spring:
aop:
auto: true
或者:
spring.aop.auto=true
并删除 @EnableAspectJAutoProxy
,但这会停止我们所有其他方面/连接。
这是我要禁用的,我该怎么做
@Aspect
@Component
public class SomeAspect {
@Around("@annotation(someAnnotation)")
public Object doSomething(ProceedingJoinPoint joinPoint, SomeAnnotation sa) throws Throwable {
// ...
}
//others
}
为了在方面 class 内动态停用单个建议,您可以使用 if()
切入点。
如果您想根据条件完全禁用某个方面(或任何其他 Spring bean 或组件),例如application.config
中的 属性,看看 @Conditional
及其特殊情况 @ConditionalOn*
。例如:
@Aspect
@Component
@ConditionalOnProperty(prefix = "org.acme.myapp", name = "aspect_active")
public class SomeAspect {
// ...
}
application.config
中的类似内容会停用方面:
org.acme.myapp.aspect_active=false
如果应用程序配置中根本没有这样的 属性,则该方面也将处于非活动状态。如果您更愿意默认为活动方面,只需使用
@ConditionalOnProperty(prefix = "org.acme.myapp", name = "aspect_active", matchIfMissing = true)
您可以按照 javadoc 中的描述进一步微调行为。
另请参阅:
- https://www.baeldung.com/spring-conditional-annotations
- https://www.baeldung.com/spring-conditionalonproperty
更新:
In order to dynamically deactivate a single advice inside an aspect class, you can use an
if()
pointcut.
糟糕,抱歉,我是 AspectJ 的原生用户,忘记了 Spring AOP 不支持 if()
切入点指示符。所以你能做的最好的事情可能是在建议的开头使用 if
表达式,具体取决于 @Value
属性.
@Value("${org.acme.myapp.advice_active:false}")
private boolean adviceActive;
@Around("@annotation(someAnnotation)")
public Object doSomething(ProceedingJoinPoint joinPoint, SomeAnnotation sa) throws Throwable {
// Skip advice logic if inactive, simply proceed and return original result
if (!adviceActive)
return joinPoint.proceed();
// Regular advice logic if active
System.out.println(joinPoint);
// Either also proceed or do whatever else is the custom advice logic, e.g.
// - manipulate method arguments,
// - manipulate original return value,
// - skip proceeding to the original method altogether and return something else.
return joinPoint.proceed();
}
当然,如果您需要那种粒度,您也可以使用我的原始解决方案,将您希望停用的建议分解为一个单独的方面 class。这样麻烦会少一些,建议方法的代码也更易读。