Rama Arjun Rama Arjun - 1 month ago 10
Java Question

Spring not able to find classpath resource with @PropertySource

I am using

@PropertySource
in my datasource configuration file to get property files located on classpath. Below is my project structure.

enter image description here

I believe I can do it in two ways:


  1. By creating a package in
    src
    folder and add them there. As
    src
    folder is already included in the classpath in eclipse, following should work.




@PropertySources({
@PropertySource("classpath: com/spring/property/general.properties"),
@PropertySource("classpath: com/spring/property/hibernate.properties")
})



  1. Second way is to create a
    resources
    folder and add it to the classpath and following should work




@PropertySources({
@PropertySource("classpath: general.properties"),
@PropertySource("classpath: hibernate.properties")
})


In my case neither of the two is working. Being an intermediate java developer this still confuses me. Can anybody guide me in the right direction. And also how we can configure classpath resources for Spring in a production environment.

EDIT:
I have changed my project structure to include properties file in
src/java/resources
and I can see the
resources
folder in build path. Still
.properties
are not found by spring.

enter image description here

Answer

For anybody facing problem with usign .properties files in Spring 4+, look at the thread below to match your setup with that of OP. Setup is all good except for a whitespace in configuration.

Not able to inject .properties file into Spring MVC 4.3 using @PropertySource

Comments