Spring - 使用静态最终字段(常量)进行 bean 初始化
是否可以使用 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";
}
如果可能,最好的方法是什么?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
像这样的东西(Spring 2.5)
其中
util
命名空间来自xmlns:util="http://www.springframework.org/schema/util"
但对于 Spring 3,使用@Value注释和表达式语言会更清晰。看起来像这样:
Something like this (Spring 2.5)
Where
util
namespace is fromxmlns:util="http://www.springframework.org/schema/util"
But for Spring 3, it would be cleaner to use the
@Value
annotation and the expression language. Which looks like this:或者,作为替代方案,直接在 XML 中使用 Spring EL:
这具有使用命名空间配置的额外优势:
Or, as an alternative, using Spring EL directly in XML:
This has the additional advantage of working with namespace configuration:
不要忘记指定架构位置..
don't forget to specify the schema location..
为上面的实例添加另一个示例。这就是如何使用 Spring 在 bean 中使用静态常量。
One more example to add for the instance above. This is how you can use a static constant in a bean using Spring.