Ondřej Míchal
Ondřej Míchal

Reputation: 623

Injecting object into Spring Configuration

I am turning old xml/java configuration into pure java config. In xml I used injection of parameters into configuration file like this:

<bean class="com.project.SpringRestConfiguration">
    <property name="parameters" ref="parameters" />
</bean>



@Configuration
public class SpringRestConfiguration {

    private Parameters parameters;

    public void setParameters(Parameters parameters) {
        this.parameters = parameters;
    }

    // @Bean definitions
    ...
}

Is it possible to inject Parameters in javaconfig? (Without the need of using autowiring!)

@Configuration
@Import(SpringRestConfiguration.class)

EDIT: With @Import I can't see any chance to inject Parameters into SpringRestConfiguration

Upvotes: 5

Views: 3090

Answers (2)

Avi
Avi

Reputation: 21760

Basically you would need to use @Autowired but you can still use a name and not type interpretation like this:

@Configuration
public class SpringRestConfiguration {

    @Autowired
    @Qualifier("parameters") // Somewhere in your context you should have a bean named 'parameters'. It doesn't matter if it was defined with XML, configuration class or with auto scanning. As long as such bean with the right type and name exists, you should be good.
    private Parameters parameters;

    // @Bean definitions
    ...
}

This solves the confusion problem you mentioned when using @Autowired - there's no question here which bean is injected, the bean that is named parameters.

You can even do a little test, leave the parameters bean defined in the XML as before, use @Autowired, see that it works. Only then migrate parameters to @Configuration class.

In my answer here you can find a complete explanation of how you should migrate XML to @Configuration step by step.

You can also skip the private member altogether and do something like this:

@Configuration
public class SpringRestConfiguration {

    @Bean
    public BeanThatNeedsParamters beanThatNeedsParamters (@Qualifier("parameters") Parameters parameters) {
       return new BeanThatNeedsParamters(parameters)
    }

}

Upvotes: 1

Karthik
Karthik

Reputation: 5040

If I have understood your question properly, this is what you are trying to do :

@Component
public class SomeConfiguration {
   @Bean(name="parameters")
   public Parameters getParameters(){
      Parameters parameters = new Parameters();
      // add your stuff
      return parameters;
   }

   @Bean(name="springRestConfiguration")
   public SpringRestConfiguration springRestConfiguration(){
      SpringRestConfiguration springRestConfiguration = new SpringRestConfiguration();
      springRestConfiguration.setParametere(getParameters());
     return springRestConfiguration;
   }

}

and use it like :

ApplicationContext appContext = new AnnotationConfigApplicationContext(SomeConfiguration.class);
SpringRestConfiguration springRestConfiguration = (SpringRestConfiguration) appContext.getBean("springRestConfiguration");

Upvotes: 0

Related Questions