Spring-使用静态final字段(常量)进行bean初始化 [英] Spring - using static final fields (constants) for bean initialization
本文介绍了Spring-使用静态final字段(常量)进行bean初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用CoreProtocolPNames类的static final字段来定义bean,如下所示:
is it possible to define a bean with the use of static final fields of CoreProtocolPNames class like this:
<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";
}
如果可能的话,最好的方法是什么?
If it is possible, what is the best way of doing this ?
推荐答案
类似的东西(春季2.5)
Something like this (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
批注和表达式语言会更干净.看起来像这样:
But for Spring 3, it would be cleaner to use the @Value
annotation and the expression language. Which looks like this:
public class Bar {
@Value("T(java.lang.Integer).MAX_VALUE")
private Integer myValue;
}
这篇关于Spring-使用静态final字段(常量)进行bean初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文