将外部属性注入Spring上下文

Der*_*ike 5 java spring

我在Spring 2.5托管项目中有三个应用程序,它们共享一些代码并且细节不同.

每个应用程序都有一个property(java.lang.String),它在构建应用程序上下文之前使用.

构建应用程序上下文需要一些时间,不能先发生.因此,它在每个单独的应用程序中定义.此属性在上下文定义中重复,因为在那里也需要它.我可以摆脱那种重复吗?

是否可以将该属性注入我的应用程序上下文?

Noe*_*l M 5

看看PropertyPlaceholderConfigurer.

Spring文档在这里讨论它.

<bean id="myPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:my-property-file.properties"/>
    <property name="placeholderPrefix" value="$myPrefix{"/>
</bean>

<bean id="myClassWhichUsesTheProperties" class="com.class.Name">
    <property name="propertyName" value="$myPrefix{my.property.from.the.file}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

然后,您可以将该String引用到您在应用程序上下文中的任何位置constructor-arg,property等等.