lis*_*sak 77 spring definition javabeans
是否可以使用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>
Run Code Online (Sandbox Code Playgroud)
public interface CoreProtocolPNames {
public static final String PROTOCOL_VERSION = "http.protocol.version";
public static final String HTTP_ELEMENT_CHARSET = "http.protocol.element-charset";
}
Run Code Online (Sandbox Code Playgroud)
如果有可能,最好的方法是什么?
Pau*_*zie 108
像这样的东西(Spring 2.5)
<bean id="foo" class="Bar">
<property name="myValue">
<util:constant static-field="java.lang.Integer.MAX_VALUE"/>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
cr7*_*ph7 25
或者,作为替代方案,直接在XML中使用Spring EL:
<bean id="foo1" class="Foo" p:someOrgValue="#{T(org.example.Bar).myValue}"/>
Run Code Online (Sandbox Code Playgroud)
这具有使用命名空间配置的额外优势:
<tx:annotation-driven order="#{T(org.example.Bar).myValue}"/>
Run Code Online (Sandbox Code Playgroud)
sam*_*ath 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>
Run Code Online (Sandbox Code Playgroud)
为上面的实例添加另一个示例。这就是如何使用 Spring 在 bean 中使用静态常量。
<bean id="foo1" class="Foo">
<property name="someOrgValue">
<util:constant static-field="org.example.Bar.myValue"/>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
77306 次 |
最近记录: |