Announcement Announcement Module
Collapse
No announcement yet.
Inject values from properties file in bean at initialization Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Inject values from properties file in bean at initialization

    Hi! How I can inject values from properties file in bean at initialization ?

    I try like this:

    Properties file
    Code:
    properties.val1=new
    properties.val2=old
    app-context.xml
    Code:
        <util:properties id="properties" location="classpath:production.properties" />
    
        <context:component-scan base-package="package" />
    Class
    Code:
    @Service
    public Class{
        .....
    
        @Value("#{properties['properties.val1']}")
        private String val1;
    
        @Value("#{properties['properties.val2']}")
        private String val2;
    
        @Autowired
        public Class(ConnectionFactory factory){
            logger.info("connectionFactory = {}, val1 = {}, val2 = {}", new Object[]{factory, val1, val2}); // val1 and val2 = null       
        }
        .....
    }
    At initialization spring context (when creating instance of bean), this value = null, but if you get this bean from context, this value are != null.
    Last edited by malahov; Apr 26th, 2013, 03:10 AM.

  • #2
    Hi

    Did you try to do that with the property placeholder bean factory post processor ?
    http://static.springsource.org/sprin...lderconfigurer

    As it is launched during the bean factory post processing phase, your values should be ready before the beans initialization phase.

    Comment


    • #3
      Hi

      The easiest way to access a properties file for configuring beans is by using PropertyPlaceholderConfigurer as follows:

      First, in your beans configuration file you have to register an instance of a factory bean post processor named PropertyPlaceholderConfigurer like this

      <bean class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
      <property name="location">
      <value>yourfile.properties</value>
      </property>
      </bean>

      Since Spring 2.5 you can replace the previous code with this:

      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schem...-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">



      <contextroperty-placeholder location="config.properties" />

      </beans>


      Now, for configuring your bean using your properties file you only have to code something like this:

      <bean id="mybean" class="my.package.MyBean">
      <property name="name" value="${name}" />
      </bean>

      Hope this help!

      BTW, I'm writing an article of this in my blog, so when it's ready I'll post the link



      EDIT:

      If in this properties file contains the configuration for multiple beans, I recommend you to use dot notation. This is an example of how the properties file should look like:

      mybean.name=John
      mybean.lastname=lastname
      mybean.age=30

      otherbean.model=123
      otherbean.country=UK

      So, your bean configuration should be changed to:

      <bean id="mybean" class="my.package.MyBean">
      <property name="name" value="${mybean.name}" />
      </bean>
      Last edited by _0R10N; Apr 30th, 2013, 07:31 AM.

      Comment


      • #4
        yes, you should use BeanFactoryPostProcessor to load the properties.

        <contextroperty-placeholder location="classpath:com/foo/jdbc.properties"/>

        Comment

        Working...
        X