lives
lives

Reputation: 1195

spring properties injection without using @Configuration bean

I want to load a property file into a spring bean. I found two approaches :

  1. configure in xml as <util.properties> and inject using @Resource or @Autowired
  2. Use @PropertySource along with the @Configuration bean.

I do not want to create @configuraiton bean for this scenario since all spring configurations are maintained in xml.

If i follow the first approach - i dont have the below option to handle the scenario of missing property file @PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)

I want the entire property file to be injected since this is lookup file and different keys will be accessed based on the logic. ( hence the context property place holder will not help )

Is there any way i can inject a property file into a bean and also handle the missing file scenario ?

Upvotes: 0

Views: 333

Answers (1)

lives
lives

Reputation: 1195

We can achieve it like below

<bean id="myLookUp" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
   <property name="ignoreResourceNotFound"><value>true</value></property>
   <property name="locations">
      <list>
        <value>classpath:myLookUp.properties</value>
      </list>
   </property>
</bean> 

Upvotes: 0

Related Questions