I have a PropertyPlaceholderConfigurer
that loads multiple properties files. I want to inject the merged properties map into a Spring Bean via config XML.
Can I do that and how?
Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects via @ConfigurationProperties . Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values.
We can read spring environment variables as well as system variables using @Value annotation. It also supports Spring Expression Language (SpEL). It is generally used for injecting values into configuration variables, which we will show and explain in the following example.
To successfully inject properties in Java EE, you need the @Produces annotation which allows the injection of primitives (such as int, long, float…). You cannot also inject classes such as String or Date because these classes are packaged in the rt. jar file which is missing a beans.
You could just create a properties bean and use that for your PropertyPlaceholderConfigurer
and your Config
bean:
<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:default.properties</value>
<value>classpath:someother.properties</value>
</list>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="myProperties" />
</bean>
<bean id="myConfigBean" class="my.pkg.Config">
<constructor-arg ref="myProperties" />
</bean>
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