Spring - 使用静态final字段(常量)进行bean初始化

83

是否可以像这样使用CoreProtocolPNames类的静态常量字段来定义一个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"; 
}
如果可能的话,最好的方法是什么?

要么删除问题,要么保持原样,但不要两者之间。谢谢。 - Pascal Thivent
5个回答

115

类似于这样的内容(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"”。

但对于Spring 3,更好的做法是使用@Value注解和表达式语言。代码如下:

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

2
也添加模式位置 xsi:schemaLocation=" http://www.springframework.org/schema/util " rel = "nofollow noreferrer">http://www.springframework.org/schema/util/spring-util-3.1.xsd"> - Sampath Pasupunuri
1
使用Spring EL进行XML配置,这样可以正常工作:#{T(com.foo.Headers).HEADER_STATUS},参考链接:http://jonstefansson.blogspot.com/2011/02/references-to-static-constants-and.html - 8bitme
1
当使用注解声明Bean时,我们如何将字段标记为私有和不可更改(final)? - Gunjan Shah
你能解释一下在你的@Value注解中T(Type)是什么意思吗?我不熟悉这种表示法。我一直使用@Value("${my.jvm.arg.name}") - Blake
我仍在努力理解“更清晰”的注释的好处 - @Value 的问题是,您必须重新发布代码才能更改配置 - 而使用 XML,则可以单独重新发布配置,或者根据环境拥有不同的配置集。 - Ed Randall
@GunjanShah:@Value也可以在@Autowired构造函数中使用。 - KIC

29
或者,作为替代方案,在XML中直接使用Spring EL:
<bean id="foo1" class="Foo" p:someOrgValue="#{T(org.example.Bar).myValue}"/>

这样做的额外好处是可以与命名空间配置一起使用:
<tx:annotation-driven order="#{T(org.example.Bar).myValue}"/>

12

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

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

6

以下是一个补充实例,与上面的示例有关。这是使用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;
    }
}

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接