복붙노트

[SPRING] Spring Jndi 컨텍스트와 PropertyPlaceholderConfigurer

SPRING

Spring Jndi 컨텍스트와 PropertyPlaceholderConfigurer

Spring을 사용하여 Websphere의 컨텍스트 내에서 변수를 읽길 원합니다.

Websphere를 사용하여 Java로 환경 변수 읽기

데이터를 정의하는 방법 .... web.xml 내부

<env-entry>
   <env-entry-name>varName</env-entry-name>
   <env-entry-value>56</env-entry-value>
   <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

자바와 함께보기

Context envEntryContext = (Context) new InitialContext().lookup("java:comp/env");
String mydata = (String)envEntryContext.lookup(“varName”);

하지만 나는 common.xml에서 데이터를 가져 가고 싶다.

<bean id="propertyPlaceholderConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>/WEB-INF/context/servweb.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders">
        <value>true</value>
    </property>
</bean>

어쩌면 그걸로

 <constructor-arg>
     <jee:jndi-lookup jndi-name="java:comp/env" default-value="data" /> 
  </constructor-arg>

하지만 그와 같은 맥락에서는

Context envEntryContext = (Context) new InitialContext().lookup("java:comp/env");
String mydata = (String)envEntryContext.lookup(“varName”);

어쩌면 그런 식으로 :

 <constructor-arg>
    <jee:jndi-lookup  jndi-name="java:comp/env">
      <jee:environment>
          varName=default
     </jee:environment>
  </jee:jndi-lookup>

올바른 방법을 아는 사람이 있습니까?

미리 감사드립니다.

해결법

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

    1.나만의 PropertyPlaceholderConfigurer를 만들 수 있습니다.

    나만의 PropertyPlaceholderConfigurer를 만들 수 있습니다.

    public class JndiPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    
        private String jndiPrefix = "java:comp/env/";
        private JndiTemplate jndiTemplate = new JndiTemplate();
    
        @Override
        protected String resolvePlaceholder(String placeholder, Properties props) {
            String value = null;
            value = resolveJndiPlaceholder(placeholder);
            if (value == null) {
                value = super.resolvePlaceholder(placeholder, props);
            }
            return value;
        }
    
        private String resolveJndiPlaceholder(String placeholder) {
            try {
                String value = (String)jndiTemplate.lookup(jndiPrefix + placeholder, String.class);
                return value;
            } catch (NamingException e) {
                // ignore
            } 
            return null;
        }
    
        public void setJndiPrefix(String jndiPrefix) {
            this.jndiPrefix = jndiPrefix;
        }
    
        public void setJndiTemplate(JndiTemplate jndiTemplate) {
            this.jndiTemplate = jndiTemplate;
        }
    }
    

    applicationContext.xml에서 사용하십시오.

    <bean id="propertyPlaceholderConfigurer"
          class="mypkg.helper.JndiPropertyPlaceholderConfigurer">
        <property name="properties">
            <props>
                <prop key="varName">default</prop>
            </props>
        </property>
    </bean>
    

    또는 속성 파일의 기본값을 정의하는 데 사용됩니다.

    <bean id="propertyPlaceholderConfigurer"
          class="mypkg.helper.JndiPropertyPlaceholderConfigurer">
        <property name="location" value="classpath:/defaults.properties"/>
    </bean>
    
  2. ==============================

    2.내 웹 응용 프로그램에서 똑같은 일을하고 있지만 InitialContext에서 읽을 수 없습니다.

    내 웹 응용 프로그램에서 똑같은 일을하고 있지만 InitialContext에서 읽을 수 없습니다.

    applicationcontext.xml에는

    <bean 
      class="com.test.webappl.JndiPropertyPlaceholderConfigurer"> 
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="location" value="file:c:\my.properties"/> 
    </bean> 
    

    my.properties에는 있습니다.

    default_mask=9999  
    

    읽으려고

    Context context = new InitialContext();
    String resource = context.lookup("java:comp/env/default_mask");
    

    그러나 컨텍스트의 바인딩에는 속성 파일이 아니라 web.xml의 env-entry 만 있습니다.

  3. ==============================

    3.컨테이너 컨텍스트에 정의 된 변수의 값을 가져 와서 자리 표시 자 객체를 만들지 않고 문자열로 사용하려는 경우 다음을 수행 할 수 있습니다 (Tomcat에서는 테스트되었지만 다른 컨테이너에서도 동일하게 작동 함). / WebSphere 같은 JEE 서버) :

    컨테이너 컨텍스트에 정의 된 변수의 값을 가져 와서 자리 표시 자 객체를 만들지 않고 문자열로 사용하려는 경우 다음을 수행 할 수 있습니다 (Tomcat에서는 테스트되었지만 다른 컨테이너에서도 동일하게 작동 함). / WebSphere 같은 JEE 서버) :

    Tomcat의 context.xml에 환경 변수를 정의하십시오 (또는 사용자 고유의 서버 구문 사용).

    <Environment type="java.lang.String" name="myString" value="hello"/>
    

    Spring XML 컨텍스트 파일에서 :

    Jee 네임 스페이스를 루트 요소에 추가하십시오.

    xmlns:jee="http://www.springframework.org/schema/jee"
    

    xsi : schemaLocation :

    http://www.springframework.org/schema/jee       http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    

    이제 당신은 쉽게 값을 찾을 수 있습니다 (java : / comp / env 항목을 지정할 필요가 없다는 것을 유의하십시오, Spring은 당신을 위해 그것을 수행합니다) :

    <jee:jndi-lookup id="myStringValue"
                         jndi-name="myStringValue"
                         expected-type="java.lang.String" />
    

    그런 다음이를 사용할 수 있습니다 (예 : bean의 생성자를 참조로 전달).

    <bean id="observationFileManager" class="my.service.Bean">
        <constructor-arg name="myString" ref="myStringValue" />
    </bean>
    

    bean은 생성자 arg로서 "hello"를 수신 할 것이다.

    편집하다 :

    통합 테스트를 위해 컨테이너 외부 (Tomcat, Websphere ...)에서 Spring 컨텍스트를 실행하면 조회가 작동하지 않습니다. 따라서 특별한 테스트 컨텍스트가있는 경우 jee를 재정의하는 다음 문자열 정의를 추가하십시오. lookup 및 테스트에 사용할 값을 설정하십시오.

    <!-- This overrides the jndi jee:lookup used in the real context -->
    <bean id="mediaFilesBaseDirPath" class="java.lang.String" >
        <constructor-arg value="Z:" />
    </bean>
    
  4. ==============================

    4.

    <bean id="propertyConfigurer" 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="location">  
    <value>classpath:resources/my-jndi.properties</value>       
    </property>  
    </bean>
    

    my-jndi.properties에서 : jndi-qconnfactory = jms / QConnFactory

    <jee:jndi-lookup id="connectionFactory" jndi-name="${jndi-qconnfactory}"/>
    
  5. from https://stackoverflow.com/questions/8998704/spring-jndi-context-and-propertyplaceholderconfigurer by cc-by-sa and MIT license