简体   繁体   English

将bean注入spring java config类中

[英]injecting beans into spring java config classes

I have a configuration class which uses the @Configuration annotation and also extends the RepositoryRestMvcConfiguration. 我有一个使用@Configuration批注的配置类,并且还扩展了RepositoryRestMvcConfiguration。

as part of the extended class, there are overridable methods that allow configuration of the bean recipes. 作为扩展类的一部分,有一些可重写的方法可以配置Bean配方。 one of which is configuring the conversion services available to the spring component. 其中之一是配置spring组件可用的转换服务。

I would like to inject some beans into a list that is iterated over and added as a conversion service through this overrided method, My configuration java class is defined below: 我想将一些bean注入到列表中,然后通过此重写的方法对其进行迭代并添加为转换服务,我的配置java类定义如下:

@Configuration
@EnableJpaRepositories(basePackages = "com.example.model.repositories")
public class DataConfig extends RepositoryRestMvcConfiguration {

    List<Converter<?,?>> converters;
    //get
    //set

    @Override
    protected void configureConversionService(ConfigurableConversionService conversionService){
        for(Converter converter : converter){
            conversionService.addConverter(converter);
        }
    }
}

The following defines my converters that i wish to inject in the app-context.xml file 下面定义了我希望注入到app-context.xml文件中的转换器

<beans>
    <bean id="fooToBarConverter" class="com.example.model.converters.FooToBarConverter" />
    <bean id="barToFooConverter" class="com.example.model.converters.BarToFooConverter" />

    <util:list id="myConverters" value-type="org.springframework.core.convert.converter.Converter">
        <ref bean="barToFooConverter"/>
        <ref bean="fooToBarConverter" />
    </util:list>
</beans>

Is there a better way of providing these converters through spring configuration or do i need to explicitly list them as output of a function contained within my configuration class like: 是否有更好的方法通过Spring配置提供这些转换器,还是我需要将它们明确列出为配置类中包含的函数的输出,例如:

@Bean
public List<Converter<?,?> myConverters(){
    Arrays.asList(new FooToBarConverter(), new BarToFooConverter());
}

Your help is highly appreciated. 非常感谢您的帮助。

PS since you are so good at spring, would you mind having a look at my spring-data-rest-mvc related question ? 附注:由于您对春季非常擅长,您介意看看与spring-data-rest-mvc相关的问题吗? please and thank you. 谢谢,麻烦您了。

By default, any @Autowired (or @Resource ) annotated Collection (or List , Set , etc) of a certain type will contain all beans of that type discovered in the context. 默认情况下,特定类型的任何@Autowired (或@Resource )批注的Collection (或ListSet等)将包含在上下文中发现的该类型的所有bean。 You could add an @Autowired in your setter and let Spring injects your controller for you. 您可以在设置器中添加@Autowired ,然后让Spring为您注入控制器。

If you need a more fine-grained control over which converters should be configured and which one should not, maybe you should configure the ConversionService altogether instead. 如果您需要更细粒度的控制,以控制应该配置哪些转换器,而不应该配置哪个转换器,则应该改为完全配置ConversionService

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM