Spring 使用@Configurable 和加载时间编织启动,@Autowire 进入非托管class
Spring Boot, @Autowire into an unmanaged class using @Configurable and load time weaving
我有一个非托管 class 的集合,我在 Spring 之外实例化了它们。我一直在尝试使用 Spring AOP,加载时间将 @Autowire
个 bean 编织到这些 classes 中,但到目前为止还没有成功。
我一直在使用 Tomcat 8 和 Spring Boot 1.2.0 进行测试。
我尝试设置 class 的 @Configuration
看起来像这样:
@Configuration
@PropertySource("classpath:application.properties")
@EnableSpringConfigured
@EnableLoadTimeWeaving
public class Config
在 Config
中,我将我想要 @Auotwire
的 bean 定义到我的非托管 classes:
@Bean
public StateProvider stateProvider() {
//setup bean
return new DynamoStateProviderImpl( );
}
非托管 bean 如下所示:
@Configurable(autowire = Autowire.BY_TYPE, dependencyCheck = true, preConstruction = true)
public class StateOutput implements UnifiedOutput {
@Autowired
private StateProvider stateProvider;
我的 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
中的东西,也没有能够从日志中提取任何信息。我还尝试使用
setter 样式注入
@Autowired
public void setStateProvider(StateProvider stateProvider){
this.stateProvider = stateProvider;
}
谢谢
为了检测 LTW,您需要使用 javaagent 或将 spring-tomcat-weaver.jar
放入 \lib
文件夹并在 context.xml
中设置 TomcatInstrumentableClassLoader
。
javaagent 示例:
-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6.SEC03/spring-agent-2.5.6.SEC03".jar
类加载器示例:
<Context>
<Loader loaderClass="org.springframework.instrument.classl oading.tomcat.TomcatInstrumentableClassLoader" />
</Context>
我有一个非托管 class 的集合,我在 Spring 之外实例化了它们。我一直在尝试使用 Spring AOP,加载时间将 @Autowire
个 bean 编织到这些 classes 中,但到目前为止还没有成功。
我一直在使用 Tomcat 8 和 Spring Boot 1.2.0 进行测试。
我尝试设置 class 的 @Configuration
看起来像这样:
@Configuration
@PropertySource("classpath:application.properties")
@EnableSpringConfigured
@EnableLoadTimeWeaving
public class Config
在 Config
中,我将我想要 @Auotwire
的 bean 定义到我的非托管 classes:
@Bean
public StateProvider stateProvider() {
//setup bean
return new DynamoStateProviderImpl( );
}
非托管 bean 如下所示:
@Configurable(autowire = Autowire.BY_TYPE, dependencyCheck = true, preConstruction = true)
public class StateOutput implements UnifiedOutput {
@Autowired
private StateProvider stateProvider;
我的 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
中的东西,也没有能够从日志中提取任何信息。我还尝试使用
@Autowired
public void setStateProvider(StateProvider stateProvider){
this.stateProvider = stateProvider;
}
谢谢
为了检测 LTW,您需要使用 javaagent 或将 spring-tomcat-weaver.jar
放入 \lib
文件夹并在 context.xml
中设置 TomcatInstrumentableClassLoader
。
javaagent 示例:
-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6.SEC03/spring-agent-2.5.6.SEC03".jar
类加载器示例:
<Context>
<Loader loaderClass="org.springframework.instrument.classl oading.tomcat.TomcatInstrumentableClassLoader" />
</Context>