Monday, August 6, 2012

Spring Util:Properties Injection via Annotations into a bean


<util:properties id="serverProperties" location="file:./applications/MyApplication/server.properties"/> 
<util:properties id="someConfig" location="file:./applications/MyApplication/config.properties"/> 

@Autowired 
@Qualifier("serverProperties") 
private Properties serverProperties; 
@Autowired 
@Qualifier("someConfig") 
private Properties otherProperties; 

or

@Resource(name = "serverProperties") 
private Properties serverProperties; 
@Resource(name = "someConfig") 
private Properties otherProperties; 

Typically, @Autowired is used for by-type autowiring in Spring, and @Resource is used for by-name. @Autowired+@Qualifier can double as by-name autowiring, but it's really meant for by-type autowiring with the ability to fine-tune the type

No comments:

Post a Comment