Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject all properties from PropertyPlaceholderConfigurer into a bean

Tags:

java

spring

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?

like image 692
naumcho Avatar asked Jun 14 '13 21:06

naumcho


People also ask

How property values can be injected directly into your beans in spring boot?

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.

Which annotation is used to inject property values into beans?

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.

How do you inject properties in Java?

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.


1 Answers

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>
like image 51
beny23 Avatar answered Oct 22 '22 14:10

beny23