2010-05-24 101 views
73

使用靜態final字段(常量)是有可能與使用CoreProtocolPNames類的靜態最終字段這樣定義一個bean:春天 - 對於Bean初始化


<bean id="httpParamBean" class="org.apache.http.params.HttpProtocolParamBean"> 
    <constructor-arg ref="httpParams"/> 
    <property name="httpElementCharset" value="CoreProtocolPNames.HTTP_ELEMENT_CHARSET" /> 
    <property name="version" value="CoreProtocolPNames.PROTOCOL_VERSION"> 
</bean> 

public interface CoreProtocolPNames { 

    public static final String PROTOCOL_VERSION = "http.protocol.version"; 

    public static final String HTTP_ELEMENT_CHARSET = "http.protocol.element-charset"; 
} 

如果可能,這樣做的最好方法是什麼?

+0

要麼刪除問題,要麼保持原樣,但不要介於兩者之間。謝謝。 – 2010-05-24 14:58:41

回答

97

像這樣的東西(Spring 2.5中)

<bean id="foo" class="Bar"> 
    <property name="myValue"> 
     <util:constant static-field="java.lang.Integer.MAX_VALUE"/> 
    </property> 
</bean> 

哪裏util命名爲xmlns:util="http://www.springframework.org/schema/util"

但對於春季3,這將是清潔使用@Value詮釋和表達語言。看起來像這樣:

public class Bar { 
    @Value("T(java.lang.Integer).MAX_VALUE") 
    private Integer myValue; 
} 
+2

還添加架構位置xsi:schemaLocation =「 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd」> – sampath 2014-03-17 22:14:34

+1

使用Spring EL for your XML config this works:#{T(com.foo.Headers).HEADER_STATUS} as http://jonstefansson.blogspot.com/2011/02/references-to-static-constants-and.html – 8bitme 2014-06-24 10:16:39

+1

當Annotation聲明bean時,我們如何將字段標記爲private和final? – 2016-11-16 07:28:57

4

爲上面的實例添加的另一個示例。這是你如何使用Spring在bean中使用靜態常量。

<bean id="foo1" class="Foo"> 
    <property name="someOrgValue"> 
    <util:constant static-field="org.example.Bar.myValue"/> 
    </property> 
</bean> 
package org.example; 

public class Bar { 
    public static String myValue = "SOME_CONSTANT"; 
} 

package someorg.example; 

public class Foo { 
    String someOrgValue; 
    foo(String value){ 
     this.someOrgValue = value; 
    } 
} 
8

不要忘記指定模式位置..

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


</beans> 
22

或者,作爲替代方案,直接在XML中使用Spring EL:

<bean id="foo1" class="Foo" p:someOrgValue="#{T(org.example.Bar).myValue}"/> 

這有使用命名空間配置的額外優勢:

<tx:annotation-driven order="#{T(org.example.Bar).myValue}"/> 
1
<util:constant id="MANAGER" 
     static-field="EmployeeDTO.MANAGER" /> 

<util:constant id="DIRECTOR" 
    static-field="EmployeeDTO.DIRECTOR" /> 

<!-- Use the static final bean constants here --> 
<bean name="employeeTypeWrapper" class="ClassName"> 
    <property name="manager" ref="MANAGER" /> 
    <property name="director" ref="DIRECTOR" /> 
</bean>