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

发布于 2024-09-02 12:37:31 字数 679 浏览 3 评论 0原文

是否可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟─花易冷 2024-09-09 12:37:31

像这样的东西(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;
}

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>

Where util namespace is from xmlns: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:

public class Bar {
    @Value("T(java.lang.Integer).MAX_VALUE")
    private Integer myValue;
}
少跟Wǒ拽 2024-09-09 12:37:31

或者,作为替代方案,直接在 XML 中使用 Spring EL:

<bean id="foo1" class="Foo" p:someOrgValue="#{T(org.example.Bar).myValue}"/>

这具有使用命名空间配置的额外优势:

<tx:annotation-driven order="#{T(org.example.Bar).myValue}"/>

Or, as an alternative, using Spring EL directly in XML:

<bean id="foo1" class="Foo" p:someOrgValue="#{T(org.example.Bar).myValue}"/>

This has the additional advantage of working with namespace configuration:

<tx:annotation-driven order="#{T(org.example.Bar).myValue}"/>
极度宠爱 2024-09-09 12:37:31

不要忘记指定架构位置..

<?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>

don't forget to specify the schema location..

<?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>
束缚m 2024-09-09 12:37:31

为上面的实例添加另一个示例。这就是如何使用 Spring 在 bean 中使用静态常量。

<bean id="foo1" class="Foo">
  <property name="someOrgValue">
    <util:constant static-field="org.example.Bar.myValue"/>
  </property>
</bean>
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;
    }
}

One more example to add for the instance above. This is how you can use a static constant in a bean using Spring.

<bean id="foo1" class="Foo">
  <property name="someOrgValue">
    <util:constant static-field="org.example.Bar.myValue"/>
  </property>
</bean>
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;
    }
}
木落 2024-09-09 12:37:31
<util:constant id="MANAGER"
        static-field="EmployeeDTO.MANAGER" />

<util:constant id="DIRECTOR"
    static-field="EmployeeDTO.DIRECTOR" />

<!-- Use the static final bean constants here -->
<bean name="employeeTypeWrapper" class="ClassName">
    <property name="manager" ref="MANAGER" />
    <property name="director" ref="DIRECTOR" />
</bean>
<util:constant id="MANAGER"
        static-field="EmployeeDTO.MANAGER" />

<util:constant id="DIRECTOR"
    static-field="EmployeeDTO.DIRECTOR" />

<!-- Use the static final bean constants here -->
<bean name="employeeTypeWrapper" class="ClassName">
    <property name="manager" ref="MANAGER" />
    <property name="director" ref="DIRECTOR" />
</bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文