Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global properties in spring

Tags:

spring

is it possible to define in a spring context file, and one or more properties that can be accessed in <bean> elements.

The example below illustrates best what I need - I want to define the property FOO once and then reference it multiple times in my various <bean> definitions:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  <properties>
    <property name="FOO" value="BAR">
  </properties>

  <bean name="TEST" class="mytest">
    <property name="MYFOO" value="${FOO}"/>
  </bean>
  <bean name="TEST1" class="mytest1">
    <property name="MYFOO" value="${FOO}"/>
  </bean>

</beans>

Any input would be much appreciated.
Thanks, Kevin.

like image 714
Kevin Avatar asked Aug 19 '09 10:08

Kevin


1 Answers

You can do this using the snappily-named PropertyPlaceHolderConfigurer. See here for the example in the spring docs. You don't define the property values themselves in the spring beans file, you externalise them in a .properties file.

You could, I suspect, use PropertyPlaceHolderConfigurer or one of its siblings to inject a Properties object defined inside your Spring file, but that would be a rather more verbose solution.

like image 70
skaffman Avatar answered Oct 02 '22 13:10

skaffman