Autowiring configuration from property files
In the recently released 1.0-beta-3 release of the Environment Aware Spring Contexts project I added support for auto wiring configuration from property files into java objects.
This feature builds on the existing functionality of the EnvironmentAwareProperties class as described in previous posts, and allows for developers to use an annotation on class fields to indicate that spring should try to auto wire values from java property files, based on a property key.
I think the best way to show this feature is by a code snippet :
public class SpringBean {
@Configuration(key="aKey")
private String aRequiredProp;
@Configuration(key="anotherKey", defaultValue="aDefault")
private String aPropWithDefault;
@Configuration(key="missingKey",required=false)
private String aPropThatAllowsNullst;
@Configuration(key="keyWithDescription",description="a desctiption")
private String aPropWithDesc;
// your business code here
}
As you can see in this code snippet there are 3 properties for the @Configuration annotation
- key : The key used for lookups in the property files
- defaultValue : The value to be used if no value found for the key
- description : A description of the usage of the property. Used for documentation purposes
- required : To indicate if we expect to find a value for the key. default value is true
You will also need to tell Spring to look for the @Configuration annotation and that is done by adding a bean post processor to your configuration file as shown below :
<beans>
<bean
class=
"no.arktekk.stagedspring.properties.ConfigurationAnnotationBeanPostProcessor"/>
</beans>
Then you should be all done, and your properties should be injected where stated.
For more information on how to use this feature and other features in the project please look at the documentation pages on the project wiki
You may also want to attend my talk at javaZone this coming Wednesday where I would present this project
About this entry
You’re currently reading “Autowiring configuration from property files,” an entry on Different aspects of life
- Published:
- Thursday, September 11th, 2008 at 9:08 am
- Author:
- Kaare Nilsen
- Category:
- Java, Open Source
- Tags:
- Java, Open Source, Spring
No comments
Jump to comment form | comments rss | trackback uri