Spring Boot, @Autowire into an unmanaged class using @Configurable and load time weaving

断了今生、忘了曾经 提交于 2019-12-18 16:47:47

问题


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.

I've been testing using Tomcat 8 and Spring Boot 1.2.0.

My @Configuration where I attempt to set up class looks like this:

@Configuration
@PropertySource("classpath:application.properties")
@EnableSpringConfigured
@EnableLoadTimeWeaving
public class Config

Inside Config I define the bean I want to @Auotwire into my unmanaged classes:

@Bean
public StateProvider stateProvider() {
    //setup bean
    return new DynamoStateProviderImpl( );
}

The unmanaged bean looks like this:

@Configurable(autowire = Autowire.BY_TYPE, dependencyCheck = true,   preConstruction = true)
public class StateOutput implements UnifiedOutput {

@Autowired
private StateProvider stateProvider;

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>

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;
}

Thanks


回答1:


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 example:

-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6.SEC03/spring-agent-2.5.6.SEC03".jar

ClassLoader example:

<Context>
    <Loader loaderClass="org.springframework.instrument.classl oading.tomcat.TomcatInstrumentableClassLoader" />
</Context>


来源:https://stackoverflow.com/questions/29167500/spring-boot-autowire-into-an-unmanaged-class-using-configurable-and-load-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!