注册一个LoadTimeWeaver

LoadTimeWeaver被Spring用于在Java虚拟机(JVM)加载类时动态转换类。

要启用加载时织入,您可以将@EnableLoadTimeWeaving添加到您的一个@Configuration类中,如下例所示:

  • Java

  • Kotlin

@Configuration
@EnableLoadTimeWeaving
public class AppConfig {
}
@Configuration
@EnableLoadTimeWeaving
class AppConfig

另外,对于XML配置,您可以使用context:load-time-weaver元素:

<beans>
	<context:load-time-weaver/>
</beans>

一旦为ApplicationContext配置了LoadTimeWeaver,该ApplicationContext中的任何bean都可以实现LoadTimeWeaverAware,从而接收到加载时织入实例的引用。这在与Spring的JPA支持结合使用时特别有用,其中对JPA类转换可能需要加载时织入。请查阅LocalContainerEntityManagerFactoryBean的javadoc以获取更多详细信息。有关AspectJ加载时织入的更多信息,请参阅Spring Framework中使用AspectJ进行加载时织入