2012-03-01 89 views
4

我可以在bean的id屬性中使用SpEL嗎?我可以在bean id屬性中使用Spring EL嗎?

e.g: <bean id="#{T(com.om.m).PublicStaticFinalStringProperty}"...

這樣,它不工作,我應該改變或這是不可能的?

+0

我覺得你有你的答案是:不可能。 – duffymo 2012-03-01 10:53:08

+0

@Bax我試過了micfra的回答,我認爲它不起作用,因爲bean的id只是文字而不是表達式的輸出。你看到其他結果了嗎?謝謝 – Ittai 2012-07-03 06:58:33

回答

2

奇怪,但可能(樣本使用彈簧3.1)。不同版本的工作:

<?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" 
     xmlns:util="http://www.springframework.org/schema/util" 
     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.xsd 
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
     "> 

    <context:property-placeholder properties-ref="myProps"/> 

    <util:properties id="myProps" > 
     <prop key="x.y.z">possible</prop> 
    </util:properties> 

    <bean id="testBean" class="Bean"> 
     <property name="value" value="weird"/> 
    </bean> 

    <bean id="${x.y.z}" class="Bean"> 
     <property name="value" value="but"/> 
    </bean> 

    <bean id="#{testBean.value}" class="Bean"> 
     <property name="value" value="${x.y.z}"/> 
    </bean> 

</beans> 

Bean.java

public class Bean implements InitializingBean { 

    String value; 

    public void setValue(String value) { 
     this.value = value; 
    } 

    public void afterPropertiesSet() throws Exception { 
     System.out.println(value); 
    } 

} 
+1

我剛剛試過你的代碼,恐怕它不起作用。我讓'Bean'實現了'BeanNameAware',並且它還打印出上下文傳遞它的值,輸出是拼寫文字而不是解釋表達式。此外,我嘗試向'#{testBean.value}'value屬性注入'#{$ {xyz} .value}'或'#{possible.value}'值來查看spring是否真的知道名爲possible的bean(然後是第二個bean的解釋的id),但是它們一起使上下文失敗。 – Ittai 2012-07-03 06:57:34

+0

上面的代碼就像一個魅力。如果你發佈你的context.xml,你的bean代碼和你使用的spring的版本,這將會很有幫助。也許作爲一個新的這樣的職位? – micfra 2012-07-04 07:59:27

相關問題