accessing value of key defined in properties file - spring

I am working with maven based spring mvc project.
I have configured PropertyPlaceholderConfigurer bean as follows with the following locations property value:
my root-context.xml:
<!-- beans config-->
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:kiran.properties</value>
</list>
</property>
</bean>
and my kiran.properties:
#properties file
excel.dirpath=root/Desktop/exceluploads/kiran/
and in my CMISUploadImpl.java:
public #Value("${excel.dirpath}")
String alfrescoSpaceStore;
//accessing key defined in properties file
Sytem.out.prinln(alfrescoSpaceStore);
o/p:${excel.dirpath}
I am getting the above o/p when accessing key value in my above said java file. I am unable to access the value of key that is defined in above said properties file. I am not getting what is wrong with my above code.

Related

Yaml file has not being recognized in application.context

I´m trying to use yaml instead of properties and in every application context i have, i put the application.yml
<!-- Enable the configuration for utilization of #PropertySource & #Value
annotations -->
<bean
class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:Messages.properties</value>
<value>classpath:application.yml</value>
</list>
</property>
</bean>
However the application doesn´t recognize the file:"Cannot resolve file 'application.yml" and the properties in #Value are not being translated.
I put mannualy the application.yaml in every resource folder but doesn´t work,
what i need to do?
Thanks

Can not load property file in spring-context.xml. Property file Path is given as a placeholder in dev.properties

I am loading property file in spring-context.xml and i am giving
external property file location in
${spring.profiles.active}.properties which is in classpath and using the location as a placholder in spring-context.xml. My spring-context.xml is:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="searchSystemEnvironment" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="locations" ref="propertyConfigurerFiles" />
</bean>
<bean id="propertyConfigurerFiles" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>/WEB-INF/properties/common.properties</value>
<!--In Developemnet Enviroenment it will be dev.properties-->
<value>/WEB-INF/properties/${spring.profiles.active}.properties</value>
<!--External Property File Location as a Placeholder-->
<value>${app.config.batch.location}</value>
</list>
</constructor-arg>
</bean>
And my dev.properties is:
app.config.batch.location=E:/project/properties/config.properties
My problem is that is ${app.config.batch.location} placeholder is not
resolved in spring-context.xml and its trying to load file
${app.config.batch.location} in place of
E:/project/properties/config.properties.
I hope I explained the problem well. Please help!
Thanks in Advance!!!
You need to create bean of class PropertyPlaceHolderConfigurer.
Not just some ArrayList bean. Why do you think you need this ArrayList bean?
It seems you are using spring profiles, instead of messing with initialization time property value binding what you can do is ...
1) read the property file(profile's)
/WEB-INF/properties/${spring.profiles.active}.properties
2) create a java class that can read these property values. (don't forget to use spring profiles interfacing class)
3) as you are trying to read a property file whose location is embedded in property file(step-1), object created at step-2 will give value for key <value>${app.config.batch.location}</value>
now you can load this property file using available file reader class.
4) create Properties object and access the values in it.
Note:: if any of your bean initialization depends on key-value read at step-4, do initialization manually or create your ***custom class(servlet) that get loaded before any other class (even spring's DispactherServlet).

Spring issues with JVM Arguments

I have a normal plain servlet . I am trying to load property file from the file system using Spring ReloadableResourceBundleMessageSource class.The file location is supplied by the JVM arguments. The following is my declaration of my MessageSource bean
<bean id="xmlXpathProperties" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- check property file(s) every 600 second(10min) -->
<property name="cacheSeconds" value="600"/>
<property name="basenames">
<list>
<value>file:#{systemProperties.aircdrconfig}/cdr-airxml</value>
</list>
</property>
/bean>
If i am giving the JVM argument name with any special characters such as dots(.) or hyphen(-) for example : air.cdr.config, I am getting an exception like.
org.springframework.beans.factory.BeanExpressionException Field or property 'air' cannot be found on object of type 'java.util.Properties'
If i remove the dot symbol then it's working fine. Any idea to overcome this problem? Thanks in advance.
You will need to refer to the properties this way:
#{ systemProperties['air.cdr.config'] }

how to load different spring properties for different environment

I have two properties files. The main.properties file has a field defined
environment=DEV
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:${catalina.home}/conf/main.properties</value>
<value>WEB-INF/project.${environment}.properties</value>
</list>
</property>
However, I got this:
Could not open ServletContext resource [/WEB-INF/project.${environment}.properties]
Does any one know how to fix it?

Spring's PropertyPlaceholderConfigurer with property in a jar file

I have multiple property files I need to refer to. Below I can refer to two that are on the classpath.
How do I refer to a property file with in a jar file ?
<bean id="placeholderConfig" name="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/my_test.properties</value>
<value>classpath:config/some_other.properties</value>
</list>
</property>
If the JAR is in the classpath, then you can reference the properties file inside just like any other resource. Just specify the location of the properties file within the JAR file.

Resources