gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:01:28 25 4
gpt4 key购买 nike

是否可以使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com