Java 带工厂的Spring依赖项注入(动态值)

Java 带工厂的Spring依赖项注入(动态值),java,spring,Java,Spring,我是春天的新手 我有一个rulefactory,它将从静态方法返回一个实例 基于类型值 现在我将从主方法参数中获取类型 现在我想将参数类型传递给工厂方法getInstance 类型参数 如何做到这一点 /*工厂类,getInstance将返回RuleEvaluation的子类型,为简单起见,我没有这样做 为SingleRuleEvaluation和MassRuleEvaluation提供了实现类。基本上这两个类都实现了规则评估*/ public class RuleEvalFactory {

我是春天的新手

我有一个rulefactory,它将从静态方法返回一个实例 基于类型值

现在我将从主方法参数中获取类型

现在我想将参数类型传递给工厂方法getInstance 类型参数

如何做到这一点

/*工厂类,getInstance将返回RuleEvaluation的子类型,为简单起见,我没有这样做 为SingleRuleEvaluation和MassRuleEvaluation提供了实现类。基本上这两个类都实现了规则评估*/

public class RuleEvalFactory {


    public static RuleEvaluation getInstance(String type) {
        if (type != null && type.equals("Single")) {
            return new SingleRuleEvaluation();
        } else if (type != null &&  type.equals("mass")) {
            return new MassRuleEvaluation();
        }
        return null;
    }

}
/*在我的主类中,我需要根据类型(dyamic)在这里获取RuleEvaluation的实例 我不知道怎么做。 */

/*我的SpringXML配置文件*/

Spring xml:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="instanceMethodFactory" class="test.factory.RuleEvalFactory"> </bean>

          <!-- i dont know how to pass the dynamic type from the Myapp main
method into this constructory argument -->

      <bean id="rulefactory" factory-bean="instanceMethodFactory" factory-method="getInstance">
        <constructor-arg index="0"> </constructor-arg>
     </bean>

</beans>
springxml:
请给出springxml和myappmain方法中的代码,说明如何将类型注入工厂方法的getInstance

问候,,
Raghu

您需要在bean中指定构造函数参数

<bean id="myBean" class="A" scope="prototype">
  <constructor-arg value="0"/> <!-- dummy value --> 
</bean>

以上链接可能重复,无法回答我的问题
<bean id="myBean" class="A" scope="prototype">
  <constructor-arg value="0"/> <!-- dummy value --> 
</bean>
getBean("myBean", argument);