31

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.

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
Avanst
  • 2,115
  • 5
  • 20
  • 20
  • 2
    Why would you need to autowire it _there_? – Sotirios Delimanolis Feb 26 '15 at 16:34
  • I was simply trying to convert an xml configuration file into a Java one. I thought since I can reference the bean by id in xml, why shouldn't I utilize the autowire annotation on it within a Java configuration file? – Avanst Feb 26 '15 at 16:59

4 Answers4

43

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • That's interesting, it works exactly has you have described. Why is that? Can you point me to any Spring documentation that talks about this precise issue? Thank you very much for the answer by the way. – Avanst Feb 26 '15 at 17:07
  • @Avanst See the documentation [here](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-java-configuration-annotation). – Sotirios Delimanolis Feb 26 '15 at 17:08
  • @SotiriosDelimanolis, today I spent a couple of hard hours looking for the similar problem caused by injecting members into configuration class. – AlexR Feb 26 '15 at 17:39
  • What if `DataSource` is an interface? For example it extends spring-data's CrudRepository interface. How would you autowire in `dataSource` then? – heez Jan 11 '17 at 00:00
  • 1
    @heez `DataSource` is an interface (`java.sql.DataSource`). I don't understand your concern. – Sotirios Delimanolis Jan 11 '17 at 00:03
  • 1
    Updated link for the documentation referenced in the comment from @SotiriosDelimanolis: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-java – Paul Nov 29 '18 at 21:27
-2

For completeness sake: Yes it is technically possible to use @Autowired annotation in spring @Configuration classes, and it works the same way as for other spring beans. But the accepted answer shows how the original problem should be solved.

tkruse
  • 10,222
  • 7
  • 53
  • 80
-3

maybe you can use constructor to inject some bean from other configuration file to your configuration,while @Autowired may not work correct in the configuration file

@Configuration
public class AppConfig {
  private DataSource dataSource;

  public AppConfig(DataSource dataSource) {
    this.dataSource = dataSource;
  }

  @Bean
  public OtherBean otherBean(){
    return new OtherBean(dataSource);
  }
}
javahuang
  • 35
  • 4
-4

beans configured with @Configuration class can't be autowired ,

So you cannot use @Autowiring and @configuration together

Shruthi H R
  • 9
  • 1
  • 4