2

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

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

如何在与normalInvRules和对应的列表中包含通用规则列表prepaidInvRules

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

4

4 回答 4

5

有一个称为“集合合并”的功能可以做到这一点。请参见第 3.3.3.4.1 节。Spring 文档中的“集合合并” 或我在该主题上写的 2008 年博客文章(带有示例)

于 2012-10-06T14:17:09.993 回答
4

I like the approach suggested by @GreyBeardedGeek, just want to throw in a few more suggestions:

a. Doing it using @Configuration with the base rules list in 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;
    }
}

b. Using a custom factory bean, responsible for extending a list:

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

}

and using it this way:

<bean name="normalInvRules" class="ListExpandingFactoryBean" p:baseList-ref="commonRules">
    <property name="extendedList">
        <list>
            <ref bean="bean4"/>
        </list>
    </property>
</bean>
于 2012-10-06T14:54:19.513 回答
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>
于 2012-10-07T04:24:06.350 回答
0

您可以创建一个具有通用规则的类并将它们自动连接到其他规则中吗?那不行吗?

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

于 2012-10-06T14:33:49.173 回答