Injecting object into Spring Configuration

梦想与她 提交于 2021-02-08 15:38:26

问题


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


回答1:


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)
    }

}



回答2:


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");


来源:https://stackoverflow.com/questions/31782819/injecting-object-into-spring-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!