Annotation Type EnableLoadTimeWeaving

@Target(value=TYPE)
 @Retention(value=RUNTIME)
 @Documented
 @Import(value=LoadTimeWeavingConfiguration.class)
public @interface EnableLoadTimeWeaving
Activates a Spring LoadTimeWeaver for this application context, available asa bean with the name "loadTimeWeaver", similar to the <context:load-time-weaver>element in Spring XML.

To be used on @Configuration classes;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 LoadTimeWeaverAware interface

Any bean that implements the LoadTimeWeaverAware interfacewill then receive the LoadTimeWeaver reference automatically; for example,Spring's JPA bootstrap support.

Customizing the LoadTimeWeaver

The default weaver is determined automatically: see DefaultContextLoadTimeWeaver.

To customize the weaver used, the @Configuration class annotated with@EnableLoadTimeWeaving may also implement the LoadTimeWeavingConfigurerinterface and return a custom LoadTimeWeaver instance through the#getLoadTimeWeaver method:

 @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 theMyLoadTimeWeaver type, meaning that it can also configure the instance, e.g.calling the #addClassTransformer method. This demonstrates how the code-basedconfiguration approach is more flexible through direct programmatic access.

Enabling AspectJ-based weaving

AspectJ load-time weaving may be enabled with the aspectjWeaving()attribute, which will cause the AspectJ class transformer tobe registered through LoadTimeWeaver.addTransformer(java.lang.instrument.ClassFileTransformer). AspectJ weaving will beactivated 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-weaving is "on". This does not occur when using@EnableLoadTimeWeaving(aspectjWeaving=ENABLED). Instead you must explicitly add@EnableSpringConfigured (included in the spring-aspects module)

Since:
3.1
Author:
Chris Beams
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值