Java 将对象注入到Spring配置中

Java 将对象注入到Spring配置中,java,spring,spring-java-config,Java,Spring,Spring Java Config,我正在将旧的xml/java配置转换为纯java配置。在xml中,我将参数注入配置文件,如下所示: <bean class="com.project.SpringRestConfiguration"> <property name="parameters" ref="parameters" /> </bean> @Configuration public class SpringRestConfiguration { private P

我正在将旧的xml/java配置转换为纯java配置。在xml中,我将参数注入配置文件,如下所示:

<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
    ...
}
@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
    ...
}
@Configuration
public class SpringRestConfiguration {

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

}
编辑:
使用@Import,我看不到任何向SpringRestConfiguration注入参数的机会

如果我正确理解了您的问题,这就是您试图做的:

@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;
   }

}
然后像这样使用它:

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

基本上,您需要使用
@Autowired
,但您仍然可以使用名称,而不是这样的类型解释:

<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
    ...
}
@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
    ...
}
@Configuration
public class SpringRestConfiguration {

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

}
这解决了您在使用
@Autowired
时提到的混淆问题-这里没有问题注入了哪个bean,即名为
parameters
的bean

您甚至可以做一个小测试,让XML中定义的
参数保持不变,使用
@Autowired
,查看它是否工作。然后才将
参数
迁移到
@Configuration

在我的文章中,您可以找到一个完整的解释,说明如何逐步将XML迁移到
@Configuration

您还可以完全跳过私有成员,并执行以下操作:

<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
    ...
}
@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
    ...
}
@Configuration
public class SpringRestConfiguration {

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

}

为什么不想使用@Autowire呢?因为有时候自动布线有点混乱。例如,当我有更多相同(超级)类型的对象时。在xml中,我能够准确地说出我想要注入哪个bean。或者只是这样,您使用第三方库/配置,并且由于自动布线,您没有完全控制权(发生在我的Spring boot上),您的问题不清楚。是否要使用
参数创建
SpringRestConfiguration
对象?@xandre在autowiring中,就像通过xml一样,可以通过名称引用实现bean。可以使用@Autowired和名称。请看一下我的问题的答案:。我相信你会在那里找到你需要的东西。你是说配置可以通过@Bean和“new”导入?现在我已经试过了,但似乎不起作用。SpringRestConfiguration中定义的bean在Spring上下文中找不到。@xandre找不到是什么意思?您是否使用了
ApplicationContext appContext=new AnnotationConfigApplicationContext(SomeConfiguration.class)?我想你应该用
@Configuration
而不是
@Component
来注释
SomeConfiguration
,这样我会创建一个新的spring上下文,不是吗?但我已经有了一个web应用程序contextOK,用限定符自动连接解决了我的问题。谢谢(所有人)