注册 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 配置,该 ApplicationContext 中的任何 bean 都可以实现 LoadTimeWeaverAware,从而接收加载时织入器实例的引用。这与 Spring 的 JPA 支持 结合使用特别有用,因为加载时织入可能对 JPA 类转换是必需的。有关更多详细信息,请查阅 LocalContainerEntityManagerFactoryBean 的 javadoc。有关 AspectJ 加载时织入的更多信息,请参阅 Spring Framework 中使用 AspectJ 进行加载时织入