有没有办法在spring配置文件中重用<util:list>?</util:list>

时间:2012-10-06 13:58:44

标签: java spring

我有一堆规则要注入一组类,如下所示:

<bean id="rule1" class="com.vikdor.rules.Rule1" />
<bean id="rule2" class="com.vikdor.rules.Rule2" />
<bean id="rule3" class="com.vikdor.rules.Rule3" />
<bean id="rule4" class="com.vikdor.rules.Rule4" />
<bean id="rule5" class="com.vikdor.rules.Rule5" />

<util:list id="commonRules">
    <ref bean="rule1" />
    <ref bean="rule3" />
    <ref bean="rule5" />
</util:list>

<util:list id="normalInvRules">
   <!-- Include common rules -->
   <ref bean="rule4" />
</util:list>

<util:list id="prepaidInvRules">
   <!-- Include common rules -->
   <ref bean="rule2" />
</util:list>

如何在与normalInvRulesprepaidInvRules对应的列表中添加公共规则列表?

规则的数量(例如rule1,rule2等)更多,数字组(normalInvRules,prepaidInvRules等)也更多。所以,我想知道是否有办法避免重复通用规则,只列出特定规则并包含对公共列表的引用。

4 个答案:

答案 0 :(得分:5)

有一个名为'集合合并'的功能就是这样做的。 见3.3.3.4.1节。 Spring documentation中的“合并” 或2008 blog post (with an example) that I wrote on the subject

答案 1 :(得分:4)

我喜欢@GreyBeardedGeek建议的方法,只想提出更多建议:

一个。使用@Configuration和xml:

中的基本规则列表进行此操作
@Configuration
@ImportResource("classpath:/baseconfig.xml")
public static class RulesConfiguration{
    @Resource List<Rule> commonRules;

    @Bean
    public List<Rule> normalInvRules(){
        List<Rule> rules = new ArrayList<Rule>();
        rules.addAll(commonRules);
        rules.add(new Rule());
        return rules;
    }
}

湾使用自定义工厂bean,负责扩展列表:

class ListExpandingFactoryBean<T> implements FactoryBean<List<T>>{

    private List<T> baseList;
    private List<T> extendedList;

    @Override
    public List<T> getObject() throws Exception {
        List<T> consolidatedList = new ArrayList<T>();
        consolidatedList.addAll(baseList);
        consolidatedList.addAll(extendedList);
        return consolidatedList;
    }

    @Override
    public Class<?> getObjectType() {
        return List.class;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }
    public void setBaseList(List<T> baseList) {
        this.baseList = baseList;
    }

    public List<T> getExtendedList() {
        return extendedList;
    }

    public void setExtendedList(List<T> extendedList) {
        this.extendedList = extendedList;
    }

}

并以这种方式使用它:                                     

<bean name="normalInvRules" class="ListExpandingFactoryBean" p:baseList-ref="commonRules">
    <property name="extendedList">
        <list>
            <ref bean="bean4"/>
        </list>
    </property>
</bean>

答案 2 :(得分:1)

感谢您的回复。这就是我最终使用Collection Merging

解决的问题
<bean id="rule1" class="com.krovi.rules.Rule1" />
<bean id="rule2" class="com.krovi.rules.Rule2" />
<bean id="rule3" class="com.krovi.rules.Rule3" />
<bean id="rule4" class="com.krovi.rules.Rule4" />
<bean id="rule5" class="com.krovi.rules.Rule5" />

<bean id="commonList"
    class="org.springframework.beans.factory.config.ListFactoryBean">
    <property name="sourceList">
        <list>
            <ref bean="rule1" />
            <ref bean="rule2" />
            <ref bean="rule3" />
        </list>
    </property>
</bean>
<bean id="firstExecutorRules" parent="commonList"
    class="org.springframework.beans.factory.config.ListFactoryBean">
    <property name="sourceList">
        <list merge="true">
            <ref bean="rule4" />
        </list>
    </property>
</bean>
<bean id="secondExecutorRules" parent="commonList"
    class="org.springframework.beans.factory.config.ListFactoryBean">
    <property name="sourceList">
        <list merge="true">
            <ref bean="rule5" />
        </list>
    </property>
</bean>

答案 3 :(得分:0)

您可以使用通用规则创建一个类,并在其他规则中自动连接它们吗?那不行吗?

您可以使用XML配置采用相同的方法,并使用ref属性注入它。