注册 LoadTimeWeaver
Spring 使用 LoadTimeWeaver 在类加载到 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 的加载时织入。