【问题标题】:Spring - using static final fields (constants) for bean initializationSpring - 使用静态最终字段(常量)进行 bean 初始化
【发布时间】:2011-02-23 06:41:25
【问题描述】:

是否可以像这样使用 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 definition javabeans


【解决方案1】:

类似的东西(春季 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 EL 用于您的 XML 配置,这是可行的:#{T(com.foo.Headers).HEADER_STATUS} 根据jonstefansson.blogspot.com/2011/02/…
  • 当 bean 被 Annotation 声明时,我们如何将字段标记为私有和最终?
  • 您能解释一下T(Type) 在您的@Value 注释中的作用吗?我对那个符号不熟悉。我一直用@Value("${my.jvm.arg.name}")
  • 我仍在为“更清洁”注释的好处而苦苦挣扎 - @Value 的问题是,您必须重新发布代码才能更改配置 - 而使用 XML 时,您可以单独重新发布配置,或具有不同的配置集,具体取决于环境。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

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

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

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

      【讨论】:

        【解决方案5】:
        <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>
        

        【讨论】:

          猜你喜欢
          • 2014-10-06
          • 2019-11-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-30
          • 1970-01-01
          • 2012-12-25
          • 1970-01-01
          相关资源
          最近更新 更多