如何使用Spring bean调用不同类的静态方法?

时间:2015-11-04 21:14:56

标签: spring

如何使用Spring bean调用其他类的静态方法?

示例:

ValidationRuleFactory是具有静态方法的工厂类。

public class ValidationRuleFactory
{
    private static final Logger LOGGER = LoggerFactory.getLogger(ValidationRuleFactory.class);

    private static Map<ValidationRuleKey, List<IValidationRule>> validationRuleMap = new HashMap<ValidationRuleKey, List<IValidationRule>>();

    public static void initializeValidationRuleMap(final Map<ValidationRuleKey, List<IValidationRule>> validationRuleMap)
    {
        ValidationRuleFactory.validationRuleMap = validationRuleMap;
        LOGGER.debug("Validation Rule Map defined in the rule factory class {}", validationRuleMap);
    }

    public static List<IValidationRule> getValidationRules(final ValidationRuleKey validationRuleKey)
    {
        final List<IValidationRule> validationRules = validationRuleMap.get(validationRuleKey);
        return validationRules;
    }
}

在ValidationRuleEngine中调用静态方法ValidationRuleFactory.getValidationRules。 但是,我需要初始化我在Spring bean中执行的validationRuleMap。 如果没有将validationRuleFactory作为ValidationRuleEngine中的实例变量,我们可以这样做吗?

公共类ValidationRuleEngine {

private static final Logger LOGGER = LoggerFactory.getLogger(ValidationRuleEngine.class);

private ValidationRuleFactory validationRuleFactory;

public void setValidationRuleFactory(final ValidationRuleFactory validationRuleFactory)
{
    this.validationRuleFactory = validationRuleFactory;
}

public List<AccrualBase> processRule(final List<AccrualBase> itemList)
{
    final List<IValidationRule> validationRuleList = ValidationRuleFactory
            .getValidationRules(new ValidationRuleKey("TestValue12"));

    for (final IValidationRule validationRule : validationRuleList)
    {
        validationRule.applyValidationRule(itemList);
    }

    return itemList;
}

}

Spring Bean:

<b:bean id="validationRuleEngine" class="package.structure.ValidationRuleEngine">
    <b:property name="validationRuleFactory">
        <b:bean class="package.structure.ValidationRuleFactory">
            <b:property name="validationRuleMap">
                <b:map>

                    <b:entry>
                        <b:key>
                            <b:bean class="package.structure.ValidationRuleKey">
                                <b:property name="keyAttribute" value="TestValue12"/>
                            </b:bean>
                        </b:key>

                        <b:list>
                            <b:ref bean="validationRuleBean1"/>
                            <b:ref bean="validationRuleBean2"/>
                        </b:list>
                    </b:entry>

                    <b:entry>
                        <b:key>
                            <b:bean class="package.structure.ValidationRuleKey">
                                <b:property name="keyAttribute" value="TestValue34"/>
                            </b:bean>
                        </b:key>

                        <b:list>
                            <b:ref bean="validationRuleBean3"/>
                            <b:ref bean="validationRuleBean4"/>
                        </b:list>
                    </b:entry>                      

                </b:map>
            </b:property>
        </b:bean>
    </b:property>
</b:bean>

0 个答案:

没有答案