Autowire a bean within Spring's Java configuration

Avanst :

Is it possible to use Spring's @Autowired annotation within a Spring configuration written in Java?

For example:

@Configuration
public class SpringConfiguration{

   @Autowired 
   DataSource datasource;

   @Bean
   public DataSource dataSource(){
       return new dataSource();
   }

   // ...

}

Obviously a DataSource interface cannot be directly instantiated but I directly instantiated it here for simplification. Currently, when I try the above, the datasource object remains null and is not autowired by Spring.

I got @Autowired to work successfully with a Hibernate SessionFactory object by returning a FactoryBean<SessionFactory>.

So my question specifically: is there a way to do that with respect to a DataSource? Or more generally, what is the method to autowire a bean within a Spring Java Configuration?

I should note I am using Spring version 3.2.

Sotirios Delimanolis :

If you need a reference to the DataSource bean within the same @Configuration file, just invoke the bean method.

@Bean
public OtherBean someOtherBean() {
    return new OtherBean(dataSource());
}

or have it autowired into the @Bean method

@Bean
public OtherBean someOtherBean(DataSource dataSource) {
    return new OtherBean(dataSource);
}

The lifecycle of a @Configuration class sometimes prevents autowiring like you are suggesting.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot autowire main's @Configuration's @Bean into a test @Component

Spring Boot can't autowire map bean in @configuration class

Spring bean with @Autowire in superclass

Autowire Enum in Spring Bean

Spring Bean and Autowire to datasource

Spring - Java configuration @Bean parameter

Spring Kafka bean configuration- why invoking bean method instead of autowire?

Autowire a Spring bean in a Singleton class

Autowire specific implementations of persistence layer in Spring with Java based configuration

Java LocalDate is not working within Spring bean

Is it necessary in a @Configuration class to autowire an @Bean for use in that class?

Calling a @Bean annotated method in Spring java configuration

Spring HttpRemoting client as a Java Configuration Bean

Autowiring YML/Java Configuration into a Spring Boot Bean

Spring MVC Bean Validation with Java Configuration

Autowire Spring Bean into interface for default method

how to autowire bean in the servlet filters in spring application?

Unable to autowire spring bean in log appender

How do I manually autowire a bean with Spring?

How to Autowire Bean of generic type <T> in Spring?

Spring autowire a stubbed service - duplicate bean

Spring: Autowire bean that does not have qualifier

How to Autowire a OkHttpClient bean in Spring Boot?

Spring autowire only if bean is present as method argument

How to define which bean to autowire in Spring

Spring: autowire bean with same type as existing class

Not able to autowire a bean using constructor in Spring

Autowire a bean from spring config - Environment specific

@Autowire Spring Bean with Injected Constructor args?