Spring Boot,使用@Configurable和加载时间编织将@Autowire转换为非托管类 [英] Spring Boot, @Autowire into an unmanaged class using @Configurable and load time weaving
问题描述
我有一组非托管类,我在Spring之外实例化。我一直在尝试使用Spring AOP将加载时间编织到 @Autowire
这些类中的bean,但到目前为止还没有运气。
I have a collection of unmanaged classes that I are instantiated outside of Spring. I've been attempting to use Spring AOP with load time weaving to @Autowire
a bean into these classes but have so far not had any luck.
我一直在测试使用Tomcat 8和Spring Boot 1.2.0。
I've been testing using Tomcat 8 and Spring Boot 1.2.0.
我的 @Configuration
我尝试设置类的位置如下所示:
My @Configuration
where I attempt to set up class looks like this:
@Configuration
@PropertySource("classpath:application.properties")
@EnableSpringConfigured
@EnableLoadTimeWeaving
public class Config
内部配置
我将我想要的bean @Auotwire
定义到我的非托管类:
Inside Config
I define the bean I want to @Auotwire
into my unmanaged classes:
@Bean
public StateProvider stateProvider() {
//setup bean
return new DynamoStateProviderImpl( );
}
非托管bean如下所示:
The unmanaged bean looks like this:
@Configurable(autowire = Autowire.BY_TYPE, dependencyCheck = true, preConstruction = true)
public class StateOutput implements UnifiedOutput {
@Autowired
private StateProvider stateProvider;
我的pom里面有以下deps
And I have the following deps inside my pom
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-agent</artifactId>
<version>2.5.6.SEC03</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
到目前为止,我还没有看到注入 stateProvider的任何内容/ code>或者能够从日志中提取任何信息。我还尝试使用
So far, I have not been able to see anything injected into stateProvider
or been able to pull any info from the logs. I've also attempted setter style injection using
@Autowired
public void setStateProvider(StateProvider stateProvider){
this.stateProvider = stateProvider;
}
谢谢
推荐答案
为了测试LTW,您需要使用javaagent或在<$中放置 spring-tomcat-weaver.jar
c $ c> \lib 文件夹并在 context.xml
中设置 TomcatInstrumentableClassLoader
。
In order to instrument LTW you'll need to either use the javaagent or place spring-tomcat-weaver.jar
in the \lib
folder and set up TomcatInstrumentableClassLoader
in context.xml
.
javaagent示例:
-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6.SEC03/spring-agent-2.5.6.SEC03".jar
ClassLoader示例:
<Context>
<Loader loaderClass="org.springframework.instrument.classl oading.tomcat.TomcatInstrumentableClassLoader" />
</Context>
这篇关于Spring Boot,使用@Configurable和加载时间编织将@Autowire转换为非托管类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!