Search This Blog

Sunday, 12 August 2012

Property files and @Value

In the previous post we saw how property file values could be used to configure our beans in the XML files. Spring has also come up with annotations to allows us to configure our bean's properties via code:
public class Properties {
    @Value("${age}")
    private int age;
    @Value("${name}")
    private String name;
    @Value("${officialName}")
    private String officialName;
//setter getter methods
}
The Annotation is used at the field or method/constructor parameter level. It indicates a default value expression for the affected argument. In this case the default value will be the value present in the property file.
name= Rajat Singh
age= 20
officialName= Mr ${name}
The third property is a special one as it is a combination of another property. Spring's property loader is perfectly capable of decoding the expression. I tested the code
public static void main(String[] args) {
    final ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            "spring-properties.xml");
    
    Properties props = (Properties) applicationContext.getBean("props");
    System.out.println(props.getOfficialName() +" of age " + props.getAge());
}
The output indicates the correct values were loaded:
Mr Rajat Singh of age 20
 
There are some more attributes available in property-placeholder element.
<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"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-3.0.xsd" >

    <context:annotation-config/>
    <context:property-placeholder location="classpath:name.properties"
        ignore-resource-not-found="false" local-override="false"
        ignore-unresolvable="false" properties-ref="defaultConfiguration"/>
    
    <util:properties id="defaultConfiguration">
        <prop key="name">Anonymous</prop>
        <prop key="age">25</prop>
        <prop key="officialName">missing</prop>
    </util:properties>
</beans>
The properties here include:
  1. ignore-resource-not-found - The value if set to false (default value) will throw an exception if the property file is missing.
  2. ignore-unresolvable - The value if set to false(default value) will result in exceptions if a particular property is not found
  3. local-override - The value if false (default) indicates that any local default values can override the property file values
  4. properties-ref - This refers to a properties bean from where the values will loaded if the properties are missing. 
I commented my properties and executed the test method again. The results indicate that the values in the defaultConfiguration bean where used.
missing of age 25

1 comment:

  1. To my point of view that's what I call a terrific article! Do you run this blog for private aims exclusively or you basically use it as a source of income?

    ReplyDelete