Spring 如何在没有javaagent的情况下使用加载时编织?
我试图在没有aspectweaver和spring工具的javaagent jar文件的情况下启用loadtimeweaving。这是我为实现同样的目标而实施的,但它不起作用Spring 如何在没有javaagent的情况下使用加载时编织?,spring,spring-boot,aspectj,load-time-weaving,compile-time-weaving,Spring,Spring Boot,Aspectj,Load Time Weaving,Compile Time Weaving,我试图在没有aspectweaver和spring工具的javaagent jar文件的情况下启用loadtimeweaving。这是我为实现同样的目标而实施的,但它不起作用 @ComponentScan("com.myapplication") @EnableAspectJAutoProxy @EnableSpringConfigured @EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.Aspect
@ComponentScan("com.myapplication")
@EnableAspectJAutoProxy
@EnableSpringConfigured
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.AUTODETECT)
public class AopConfig implements LoadTimeWeavingConfigurer {
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
return new ReflectiveLoadTimeWeaver();
}
/**
* Makes the aspect a Spring bean, eligible for receiving autowired components.
*/
@Bean
public InstrumentationLoadTimeWeaver loadTimeWeaver() throws Throwable {
InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();
return loadTimeWeaver;
}
}
我发现的一个解决方法是从
spring instrument
热连接InstrumentationSavingAgent
,而不是通过-javaagent
命令行参数启动代理。但为此,您需要一个插装实例。我刚刚使用了微型助手库byte-buddy-helper
(不用担心,它独立于ByteBuddy工作),可以做到这一点。确保在Java9+JVM中,如果由于某种原因连接API不起作用,则激活该API
@ComponentScan("com.myapplication")
@EnableAspectJAutoProxy
@EnableSpringConfigured
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.AUTODETECT)
public class AopConfig implements LoadTimeWeavingConfigurer {
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
return new ReflectiveLoadTimeWeaver();
}
/**
* Makes the aspect a Spring bean, eligible for receiving autowired components.
*/
@Bean
public InstrumentationLoadTimeWeaver loadTimeWeaver() throws Throwable {
InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();
return loadTimeWeaver;
}
}
因此,在您的配置类中去掉实现LoadTimeWeavingConfigurer
和两个工厂方法,只需这样做:
org.springframework
弹簧乐器
net.bytebuddy
字节好友代理
1.10.14
@springboot应用程序
公共类应用程序{
公共静态void main(字符串[]args){
Instrumentation Instrumentation=ByteBuddyAgent.install();
InstrumentationSavingAgent.premain(“,instrumentation”);
ConfigurableApplicationContext上下文=SpringApplication.run(Application.class,args);
// ...
}
}
如果您有任何不理解的地方,请随时提出后续问题
更新:我注意到,这只适用于启用了aspectjWeaving=ENABLED
的我,而不适用于AUTODETECT
。对于一个示例Springbean,我注意到@Component
不起作用,可能是因为Spring和AspectJ之间存在一些引导问题。因此,我用显式的@Bean
配置替换了它,然后它就工作了。大概是这样的:
@配置
@组件扫描(“com.spring.aspect.dynamicflow”)
@EnableLoadTimeWeaving(aspectjWeaving=ENABLED)
公共类应用程序配置{
@豆子
公共工作流程JobProcess(){
返回新的JobProcessImpl();
}
}