Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 460
francis Avatar asked Mar 20 '15 13:03

francis


People also ask

Can we use @autowired without @component?

So the answer is: No, @Autowired does not necessarily mean you must also use @Component . It may be registered with applicationContext. xml or @Configuration+@Bean .

Can we use @autowired in Spring?

In Spring, you can use @Autowired annotation to auto-wire bean on the setter method, constructor , or a field . Moreover, it can autowire the property in a particular bean. We must first enable the annotation using below configuration in the configuration file. We have enabled annotation injection.

Can we Autowire in Spring boot main class?

There are different ways through which we can autowire a spring bean. autowire byName - For this type of autowiring, setter method is used for dependency injection. Also the variable name should be same in the class where we will inject the dependency and in the spring bean configuration file.

What is @configurable in Spring?

One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework.


1 Answers

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>
like image 90
bvulaj Avatar answered Sep 30 '22 19:09

bvulaj