Import Spring property files from jar file on classpath

I want to import all property files ending in .properties that are contained in the src/main/resource locations of ALL the jar dependencies that my project has.

I wrote a JUnit test where my context.xml is located in the src / test / resources folder. I set the placeholder property with wildcards, but it does not work.

 <context:property-placeholder location="classpath*:*.properties"/> 

May I be stupid, but I could not find a solution to my problem on the net. Does anyone know what the correct syntax is?

EDIT:

The root project has maven dependencies that are allowed from my workspace:

enter image description here

And I want to import the module.properties files of dependent projects:

enter image description here

+6
source share
3 answers

From the Spring documentation :

The prefix "classpath *:" can also be combined with the PathMatcher template in the rest of the location path, for example, "classpath *: META-INF / * - beans.xml". [...]

But there is a limitation:

Please note that "classpath *:" in combination with Ant-like patterns will work reliably with only one root directory before starting the pattern, unless the actual target files are on the file system. This means that a template such as "classpath *: *. Xml" will not extract files from the root of jar files, but rather from the root of extended directories. [...]

Therefore, if I put my module properties files in src / main / resources / META-INF, I can load them as follows:

 <context:property-placeholder location="classpath*:/META-INF/*.properties" /> 
+7
source

You can also do it like this:

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:your.properties</value> <value>classpath*:your.properties</value> ..... </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true"/> </bean> 

more complex example:

 <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreResourceNotFound" value="true" /> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="locations"> <list> <value>classpath*:properties/defaults.properties</value> <value>classpath*:properties/${props.env.name}.properties</value> <value>classpath*:com/calciuum/config/defaults.properties</value> <value>classpath*:com/calciuum/config/${props.env.name}.properties</value> <value>classpath*:${props.env.classpath}/defaults.properties</value> <value>classpath*:${props.env.classpath}/${props.env.name}.properties</value> <value>file:${props.env.ext.properties}</value> </list> </property> </bean> 
+4
source

if the properties you use are less than 4. You can use this:

 <context:property-placeholder location="classpath:test1.properties,classpath:test2.properties" /> 

Just use this

 <context:property-placeholder location="classpath:*.properties" /> 
+1
source

Source: https://habr.com/ru/post/970683/


All Articles