2010-05-24 10 views
73

ist es möglich, eine Bohne mit der Verwendung von static final Bereichen CoreProtocolPNames Klasse wie folgt zu definieren:Spring - mit static final Felder (Konstanten) für Bohnen Initialisierung


<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"; 
} 

Wenn es möglich ist, was ist der beste Weg, dies zu tun?

+0

Löschen Sie entweder die Frage oder lassen Sie sie unverändert, aber nicht dazwischen. Vielen Dank. –

Antwort

97

So etwas (Frühjahr 2,5)

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

Wo util Namespace von xmlns:util="http://www.springframework.org/schema/util"

ist aber für den Frühling 3, wäre es sauberer sein, die @Value Annotation und die Ausdruckssprache zu verwenden. Das sieht so aus:

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

Fügen Sie auch den Schema-Speicherort hinzu xsi: schemaLocation = " http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> – sampath

+1

Verwenden Spring EL für Ihre XML-Konfiguration funktioniert das: # {T (com.foo.Headers) .HEADER_STATUS} gemäß http://jonstefansson.blogspot.com/2011/02/references-to-static-constants-and.html – 8bitme

+1

Wie können wir ein Feld als privat und endgültig markieren, wenn Bean von Annotation deklariert wird? –

4

Ein weiteres Beispiel für die obige Instanz hinzufügen. So können Sie eine statische Konstante in einer Bean mit Spring verwenden.

<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

vergessen Sie nicht das Schema Speicherort angeben ..

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

Oder als Alternative, mit Spring EL direkt in XML:

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

Dies hat Der zusätzliche Vorteil der Arbeit mit Namespace-Konfiguration:

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