是否可以使用 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";
}
如果可能,最好的方法是什么?
最佳答案
类似的东西( 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;
}
关于Spring - 使用静态最终字段(常量)进行 bean 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2897819/