有条件的" ref"在SPRING豆

时间:2016-01-08 14:09:43

标签: java spring spring-mvc

如果我有类似

的话
<bean id="helloWorld" class="com.askQuestion.HelloWorld">
       <property name="message" ref = (postCard or formalLetter )if something />
   </bean>

...

 <bean for postCard class />
  <bean for formalLetter class />

在类HelloWorld中,消息是这样的接口:

MessageInterface message ; // also get; set are here

还有两个类 - class postCard implements MessageInterfaceclass formalLetter implements MessageInterface 并且必须使用a初始化类message中的属性HelloWorld 如果某些类中的某些值(例如com.askQuestion.ClassWithAConditional)具有例如true值,则使用postCard bean或者使用formalLetter?

所以如果 - com.askQuestion.ClassWithAConditional.SendFormalLetter == true; 那么

<bean id="helloWorld" class="com.askQuestion.HelloWorld">
           <property name="message" ref = "formalLetterId" />
       </bean>

如果com.askQuestion.ClassWithAConditional.SendFormalLetter == false; 那么

<bean id="helloWorld" class="com.askQuestion.HelloWorld">
               <property name="message" ref = "postCardId" />
           </bean>

2 个答案:

答案 0 :(得分:2)

I would say you have 2 options.

  1. Use Spring Profiles to load one of the beans based on an active profile
  2. Use a FactoryBean to switch between the different implementations.

Profiles

When using Spring you can use profiles. In xml declare a nested beans element and set which profile it should be active in. The active profile can be set by setting the spring.profiles.active property.

<beans ...>
    <bean id="helloWorld" class="com.askQuestion.HelloWorld">
        <property name="message" ref="messageBean" />
    </bean>    

    <beans profile="postcard">
        <bean id="messageBean" class="PostcardBean" />
    <beans>
    <beans profile="letter">
        <bean id="messageBean" class="LetterBean" />
    </beans>
</beans>

More information on profiles is in the reference guide内的ActionLink。

的FactoryBean

您还可以定义FactoryBean,根据您的条件选择要使用的正确bean。

public class MessageFactoryBean implements FactoryBean<MessageInterface> {

    private MessageInterface postcard;
    private MessageInterface letter;

    public MessageInterface getObject() {
        if (your condition here) {
            return letter;
        }
        return postcard;
    }
    // other methods omitted
}

<beans ...>
    <bean id="messageBean" class="MessageFactoryBean">
        <property name="postcard">
            <bean class="PostcardBean" />
        </property>
        // other properties omitted
    </bean>

    <bean id="helloWorld" class="com.askQuestion.HelloWorld">
        <property name="message" ref="messageBean"/>
    </bean>    
</beans>

有关the reference guideFactoryBean的更多信息。

答案 1 :(得分:1)

您可以像这样使用FactoryBean

public class MyFactoryBean implements FactoryBean<MessageInterface>{
  private boolean condition; 

  public void setCondition(boolean condition){ this.condition =condition ; }

  public MessageInterface getObject(){ 
    if(condition){return new PostCard();}
    else{return new formalLetter();}
  }

  public Class<MessageInterface> getObjectType() { return MessageInterface.class ; } 

  public boolean isSingleton() { return false; }
}

然后在你的配置中

<bean class = "MyFactoryBean" id = "myFactoryBean">
    <property name = "condition" value ="true"/>
</bean>
<bean id="helloWorld" class="com.askQuestion.HelloWorld">
       <property name="message" ref = "myFactoryBean" />
   </bean>