If I have 2 .properties files setup in my Spring XML as so:
<util:properties id="serverProperties" location="file:./applications/MyApplication/server.properties"/> <util:properties id="someConfig" location="file:./applications/MyApplication/config.properties"/>
How can I inject via annotations these properties files into a bean with java.util.Properties
?
How can I grab specific properties via Spring annotations?
Cheers!
Spring Bean + Properties + Annotation configuration example Create a configuration class and define the properties bean in it as shown below. Create a spring bean class and inject properties into it using @Autowired and @Qualifier annotations. Create main class and run application.
Spring get value from system property. You can set the system property on the Java command line using the -Dpropertyname=value syntax or at runtime using System. setProperty function. To inject the value from the system property, annotate your field using the @Value annotation with SpEL expression as below.
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With