Results 1 to 4 of 4

Thread: Param in property file confusion

  1. #1
    Join Date
    Apr 2011
    Posts
    17

    Default Param in property file confusion

    I have config file

    <bean id="propertyPlaceholderConfigurer"
    class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>classpath:db.properties</value>
    </list>
    </property>
    </bean>

    <bean id="loadConfig" class="Spring.LoadConfigFileTest">
    <property name="user" value="${ssss}"/>
    </bean>
    </beans>

    My db.properties looks like

    user=TR
    pwd=TR

    I have included a property from a file which is not present in .proeprty file. When i load the config file and refer to that property "user", its not throwing any errors. It prints the vale us "${ssss}".

    public class LoadConfigFileTest{

    private String user;

    public String getUser() {
    return user;
    }

    public void setUser(String user) {
    this.user = user;
    }

    public static void main(String[] args)
    {
    Resource resource = new FileSystemResource("./spring-config.xml");
    BeanFactory beanFactory = new XmlBeanFactory(resource);
    LoadConfigFileTest loadConfig = (LoadConfigFileTest)beanFactory.getBean("loadConfi g");
    System.out.println("Loaded---"+loadConfig.getUser());
    }
    }

    Ideally i should get an exception that the property being referred is not present. So what wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

    References to nonexistent properties do not result in exceptions.

    You might consider using the "@Required" annotation on the instance variable or setter. Perhaps that will work. Alternatively, you could define a @PostConstruct method to verify your settings.

  3. #3
    Join Date
    Apr 2011
    Posts
    17

    Default

    Thanks. Even if i refer to existing property from file, its not picking the value it shows {user}. Why its happening?

  4. #4
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

    Elaborate. Show details.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •