簡體   English   中英

SPRING bean中的條件“ref”

[英]Conditional “ref” in SPRING beans

如果我有類似的東西

<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並且類HelloWorld的屬性message必須用postCard bean或formalLetter初始化,如果某個類中的某些值(比如com.askQuestion.ClassWithAConditional)有例如true價值?

所以if - 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>

我會說你有兩個選擇。

  1. 使用Spring Profiles根據活動配置文件加載其中一個bean
  2. 使用FactoryBean在不同的實現之間切換。

簡介

使用Spring時,您可以使用配置文件。 在XML聲明一個嵌套beans元件和一套profile應該在活躍,有效簡表可以通過設置來設置spring.profiles.active屬性。

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

有關配置文件的更多信息, 請參閱參考指南

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

有關FactoryBean的更多信息, 請參閱參考指南

您可以像這樣使用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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM