注释类型 EnableLoadTimeWeaving
@Target(TYPE) @Retention(RUNTIME) @Documented @Import(LoadTimeWeavingConfiguration.class) public @interface EnableLoadTimeWeaving
Activates a SpringLoadTimeWeaverfor this application context, available as a bean with the name "loadTimeWeaver", similar to the<context:load-time-weaver>element in Spring XML.To be used on @
Configurationclasses; the simplest possible example of which follows:@Configuration @EnableLoadTimeWeaving public class AppConfig { // application-specific @Bean definitions ... }The example above is equivalent to the following Spring XML configuration:<beans> <context:load-time-weaver/> <!-- application-specific <bean> definitions --> </beans>The
Any bean that implements theLoadTimeWeaverAwareinterfaceLoadTimeWeaverAwareinterface will then receive theLoadTimeWeaverreference automatically; for example, Spring's JPA bootstrap support.Customizing the
The default weaver is determined automatically: seeLoadTimeWeaverDefaultContextLoadTimeWeaver.To customize the weaver used, the
@Configurationclass annotated with@EnableLoadTimeWeavingmay also implement theLoadTimeWeavingConfigurerinterface and return a customLoadTimeWeaverinstance through the#getLoadTimeWeavermethod:@Configuration @EnableLoadTimeWeaving public class AppConfig implements LoadTimeWeavingConfigurer { @Override public LoadTimeWeaver getLoadTimeWeaver() { MyLoadTimeWeaver ltw = new MyLoadTimeWeaver(); ltw.addClassTransformer(myClassFileTransformer); // ... return ltw; } }The example above can be compared to the following Spring XML configuration:
<beans> <context:load-time-weaver weaverClass="com.acme.MyLoadTimeWeaver"/> </beans>The code example differs from the XML example in that it actually instantiates the
MyLoadTimeWeavertype, meaning that it can also configure the instance, e.g. calling the#addClassTransformermethod. This demonstrates how the code-based configuration approach is more flexible through direct programmatic access.Enabling AspectJ-based weaving
AspectJ load-time weaving may be enabled with theaspectjWeaving()attribute, which will cause the AspectJ class transformer to be registered throughLoadTimeWeaver.addTransformer(java.lang.instrument.ClassFileTransformer). AspectJ weaving will be activated by default if a "META-INF/aop.xml" resource is present on the classpath. Example:@Configuration @EnableLoadTimeWeaving(aspectjWeaving=ENABLED) public class AppConfig { }The example above can be compared to the following Spring XML configuration:
<beans> <context:load-time-weaver aspectj-weaving="on"/> </beans>The two examples are equivalent with one significant exception: in the XML case, the functionality of
<context:spring-configured>is implicitly enabled whenaspectj-weavingis "on". This does not occur when using@EnableLoadTimeWeaving(aspectjWeaving=ENABLED). Instead you must explicitly add@EnableSpringConfigured(included in thespring-aspectsmodule)- 从以下版本开始:
- 3.1
- 作者:
- Chris Beams
- 另请参阅:
LoadTimeWeaver,DefaultContextLoadTimeWeaver,ClassPreProcessorAgentAdapter
可选元素概要
可选元素 修饰符和类型 可选元素 说明 EnableLoadTimeWeaving.AspectJWeavingaspectjWeavingWhether AspectJ weaving should be enabled.
元素详细资料
aspectjWeaving
EnableLoadTimeWeaving.AspectJWeaving aspectjWeaving
Whether AspectJ weaving should be enabled.- 默认值:
- org.springframework.context.annotation.EnableLoadTimeWeaving.AspectJWeaving.AUTODETECT