简体   繁体   English

如何使用与父bean分离的集合数据创建spring bean?

[英]How to create spring bean with collection data decoupled from parent bean?

Spring has ability to initialisate values of core java collection types . Spring能够初始化核心java集合类型的值

I have a complex collection type Map<String, Set<String>> map and it inital value defined in spring config: 我有一个复杂的集合类型Map<String, Set<String>> map和它在spring config中定义的初始值:

<bean id="dao" class="ru.mypkg.dao.DaoImpl">
    <property name="dataSource" ref="dataSource"/>
    <property name="map">
        <map>
            <entry key="TABLE">
                <set>
                    <value>COMMENT</value>
                    <value>INDEX</value>
                </set>
            </entry>
            <entry key="VIEW">
                <set>
                    <value>COMMENT</value>
                </set>
            </entry>
        </map>
    </property>
</bean>

I want rewrite my config in next manner: Split it on 2 beans for more readability 我想以下一种方式重写我的配置:将它拆分为2个bean以提高可读性

<bean id="dao" class="ru.mypkg.dao.DaoImpl">
    <property name="dataSource" ref="dataSource"/>
    <property name="map" ref-id="myMap"/>        
</bean>

<bean id="myMap" ..????..>
        <entry key="TABLE">
                <set>
                    <value>COMMENT</value>
                    <value>INDEX</value>
                </set>
            </entry>
            <entry key="VIEW">
                <set>
                    <value>COMMENT</value>
                </set>

            </entry>
</bean>

Can I achieve that with no creating additional classes? 我可以在不创建其他课程的情况下实现这一目

Certainly, using the <util:map> namespace. 当然,使用<util:map>命名空间。 See the Spring documentation C.2.2.5 . 请参阅Spring文档C.2.2.5

Another way to create complex configuration is using the @Configuration approach, or alternatively the FactoryBean interface. 创建复杂配置的另一种方法是使用@Configuration方法,或者使用FactoryBean接口。

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

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