如何基于配置属性在非Spring Boot Spring项目中启用/禁用Load Time Weaving
非Spring Boot项目下AspectJ LoadTimeWeaving配置化开关实现方案
首先明确:@EnableLoadTimeWeaving的属性属于编译期常量,无法直接运行期读取properties配置动态赋值,可通过以下两种原生Spring方案实现配置驱动的开关控制。
方案1:@Conditional + 自定义条件类(推荐,Spring 4.0+支持)
仅当配置项满足要求时,才加载携带@EnableLoadTimeWeaving的配置类,对现有业务代码侵入最小。
实现步骤
- 自定义条件判断类,读取Environment中的配置项判断是否启用LTW
import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; public class LtwEnableCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { // 自定义配置项aspectj.ltw.enabled,默认值为false即默认关闭 return context.getEnvironment().getProperty("aspectj.ltw.enabled", Boolean.class, false); } }
- 给LTW配置类加上条件注解
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.EnableLoadTimeWeaving; @Configuration @Conditional(LtwEnableCondition.class) @EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED) public class AspectjLtwConfig { // 原有的LTW相关配置 }
- 在properties配置文件中设置开关
# 设为true启用LTW,不配置或设为false则功能关闭 aspectj.ltw.enabled=true
前置要求
确保properties配置被Spring提前加载,可通过如下配置注册属性解析器:
@Bean public static PropertySourcesPlaceholderConfigurer propertyConfigurer() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); configurer.setLocation(new ClassPathResource("你的配置文件路径.properties")); return configurer; }
低版本Spring兼容(4.0以下)
如果Spring版本低于4.0不支持@Conditional,可以用Profile实现:
- 给LTW配置类加
@Profile("ltw-enabled") - 启动时通过
spring.profiles.active=ltw-enabled参数或者properties配置项控制该Profile是否激活,即可实现LTW开关。
方案2:手动注册LTW相关Bean,代码控制开关
放弃@EnableLoadTimeWeaving注解,直接通过代码读取配置后手动注册LTW所需组件,灵活度更高:
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Bean; import org.springframework.beans.factory.annotation.Value; import org.springframework.instrument.classloading.LoadTimeWeaver; import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver; import org.springframework.context.weaving.AspectJWeavingEnabler; import org.springframework.context.weaving.LoadTimeWeavingConfigurer; import org.springframework.core.env.Environment; @Configuration public class AspectjLtwConfig implements LoadTimeWeavingConfigurer { @Value("${aspectj.ltw.enabled:false}") private boolean enableLtw; @Override public LoadTimeWeaver getLoadTimeWeaver() { return enableLtw ? new InstrumentationLoadTimeWeaver() : null; } @Bean public static AspectJWeavingEnabler aspectJWeavingEnabler(Environment env) { if (!env.getProperty("aspectj.ltw.enabled", Boolean.class, false)) { return null; } AspectJWeavingEnabler enabler = new AspectJWeavingEnabler(); enabler.setAspectJWeaving(EnableLoadTimeWeaving.AspectJWeaving.ENABLED); return enabler; } }
常见说明
注解的属性值必须是编译期可确定的常量,因此无法通过${}占位符或者其他动态方式给@EnableLoadTimeWeaving的aspectjWeaving属性直接赋值,必须通过条件化加载配置类或者手动注册Bean的方式实现动态开关。
内容的提问来源于stack exchange,提问作者saharsh-jain
相关产品推荐
相关产品推荐

