Is constructor injection possible in Spring configuration classes?

Viewed 5628

If I have a Spring configuration class (i.e. a class annotated with @Configuration) can I use constructor injection ?

As it stands if I add one I get a no default constructor message, and if I add a default constructor it uses that rather than the overloaded one, which doesn't really help.

2 Answers

In Spring 4.3, you can use org.springframework.beans.factory.ObjectProvider in @Configuration annotated class constructors to inject beans. for example:

@Configuration
public class SimpleBean {
  private final InnerBean prop1;
  public Simple Bean(ObjectProvider<InnerBean> innerBeanProvider) {
     prop1 = innerBeanProvider.getObject();
  }      
}
Related