복붙노트

[SPRING] Spring - bean 초기화를위한 정적 final 필드 (상수) 사용

SPRING

Spring - bean 초기화를위한 정적 final 필드 (상수) 사용

다음과 같이 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"; 
}

가능하다면, 이것을하는 가장 좋은 방법은 무엇입니까?

해결법

  1. ==============================

    1.이 같은 것 (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>
    

    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;
    }
    
  2. ==============================

    2.또는 대안으로 Spring EL을 XML에서 직접 사용하기 :

    또는 대안으로 Spring EL을 XML에서 직접 사용하기 :

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

    이것은 네임 스페이스 설정 작업의 추가적인 장점을 가지고 있습니다 :

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

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

    4.위의 인스턴스에 추가 할 예제가 하나 더 있습니다. 이것은 스프링을 사용하여 빈에서 정적 상수를 사용하는 방법이다.

    위의 인스턴스에 추가 할 예제가 하나 더 있습니다. 이것은 스프링을 사용하여 빈에서 정적 상수를 사용하는 방법이다.

    <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;
        }
    }
    
  5. ==============================

    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>
    
  6. from https://stackoverflow.com/questions/2897819/spring-using-static-final-fields-constants-for-bean-initialization by cc-by-sa and MIT license