3.14 注册一个加载时编织器 {#toc_16}

在类被加载进JVM时Spring使用LoadTimeWeaver进行动态转换。
为了使的load-time weaving可用,那么你只需在配置了@Configuration的类上添加@EnableLoadTimeWeaving。

  1. @Configuration
  2. @EnableLoadTimeWeaving
  3. public class AppConfig {
  4. }

相应的xml配置使用context:load-time-weaver元素:

  1. <beans>
  2. <context:load-time-weaver/>
  3. </beans>

一旦配置了ApplicationContext,那么在ApplicationContext中的任何bean都可以实现LoadTimeWeaverAware,从而接受对类加载时编织器实例的引用。这与Spring JPA支持相结合时非常有用,JPA类转化必须使用加载时编织。可以通过javadocs的LocalContainerEntityManagerFactoryBean获取更多详细信息,对于AspectJ加载时的编织请参考:

Section 7.8.4, “Load-time weaving with AspectJ in the Spring Framework”.