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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2897819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:04:48  来源:igfitidea点击:

Spring - using static final fields (constants) for bean initialization

springdefinitionjavabeans

提问by lisak

is it possible to define a bean with the use of static final fields of CoreProtocolPNames class like this:

是否可以使用 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"; 
}


If it is possible, what is the best way of doing this ?

如果可能,最好的方法是什么?

回答by Paul McKenzie

Something like this (Spring 2.5)

像这样的东西(Spring 2.5)

<bean id="foo" class="Bar">
    <property name="myValue">
        <util:constant static-field="java.lang.Integer.MAX_VALUE"/>
    </property>
</bean>

Where utilnamespace is from xmlns:util="http://www.springframework.org/schema/util"

util命名空间来自哪里xmlns:util="http://www.springframework.org/schema/util"

But for Spring 3, it would be cleaner to use the @Valueannotation and the expression language. Which looks like this:

但是对于 Spring 3,使用@Value注解和表达式语言会更干净。看起来像这样:

public class Bar {
    @Value("T(java.lang.Integer).MAX_VALUE")
    private Integer myValue;
}

回答by cr7pt0gr4ph7

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

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

<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}"/>

回答by sampath

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>

回答by Balaji Boggaram Ramanarayan

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

为上述实例添加的另一个示例。这就是使用 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;
    }
}

回答by chetan singhal

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