Monday, October 25, 2010

Annotated Property Access using the Spring Framework

Spring 3.0 introduced and annotation mechanism for property value access. Simply apply the @Value annotation to an attribute definition and the specified property value will be assigned during context initialization.

Consider the following application context property placeholder definition that loads property files located on a classpath path location.

<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>


An annotated attribute is injected with values by annotating the attribute(s) that are configurable by Spring. Notice, that property values utilize EL (entity language) notation to render a value from the property placeholder name .
 @Configurable
  
 public class Foo {
  
   @Value("${some.property}")
  
   String propertyAttribute;
  
   ..... rest of the class definition.....
  
  }
  
Entries in a property text file would look like this.
 some.property = Some Value for a property