2011-04-14 4 views
1

속성 파일의 요청에 따라 특정 값을 얻고 싶습니다. 어떻게해야합니까? 속성은 콩에 Properties를 주입런타임에 속성 파일의 가치를 읽음

<bean id="Prop" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
     <value>classpath:ErrorMessage.properties</value> 
    </property> 
</bean> 

<bean id="PropertiesBean" class="com.util.PropertiesUtil"> 
    <property name="Exprops" value="${EXampleExceptiion}"></property> 
</bean> 
+0

PropertyPlaceholderConfigurer는 어떻게 든 정적 인 응용 프로그램을 구성하기위한 것일뿐입니다. 당신이 신청할 수 있습니다. – Ralph

+0

직접 구현해야 할 것 같습니다. 제가 아는 한 그것에 대한 구체적인 지원은 없습니다. –

+0

@ Ralph, @ Benjamin..can 어느 곳에서나 PropertyPlaceholderConfigurer에서 읽은 파일을 런타임에 값을 설정하는 데 사용할 수있는 곳을 제공합니다. – Vish

답변

9

사용 PropertiesFactoryBean 파일에서 나는 봄 configuration.i 다음 한

는 요청에 따라 Exprops에 대한 값을 설정하고 해당 값을 얻을 싶어요.

<bean id="myPropertiesBean" 
    class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
    <property name="location" value="classpath:ErrorMessage.properties"/> 
</bean> 

이것은 어떤 빈 (<property name="x" ref="myPropertiesBean"/>)에서 이름 myPropertiesBean하에 주입 될 수 Properties 객체/빈을 제공한다. 또한 봄에는

은 (스프링 2.5부터)이 UTIL 네임 스페이스를 제공

<util:properties id="myPropertiesBean" 
location="classpath:ErrorMessage.properties"/> 

@see Spring Reference Chapter C.2.2.3.

+0

감사합니다. @ Ralph,이 모양이 더 좋습니다. – Vish

+0

동일한 PropertiesFactoryBean에 여러 속성을 쓸 수 있습니까? 다른 스프링 빈에 의해 사용된다. – Vish

+0

@Vish : PropertiesFactoryBean은 java.util.Properties를 제공한다. (또는 당신은 복수 재산 파일을 의미 했습니까?). 네, 서로 다른 빈에서 사용할 수 있습니다 (각각은 java.util.Properties의 인스턴스 하나를 갖지만 키/값은 같습니다) – Ralph

1

모두이 일에 대해 다음을 사용 : 이 당신이 조금 짧은 PropertyFactoryBean 정의를 쓸 수 있습니다 프로그래밍 방식으로

XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml")); 
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); 
cfg.setLocation(new FileSystemResource("jdbc.properties")); 
cfg.postProcessBeanFactory(factory); 
관련 문제