Spring issues with JVM Arguments - spring

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'] }

Related

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).

accessing value of key defined in properties file

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.

Spring PropertyPlaceholderConfigurer not replacing placeholder

I want to pass the WSDL url for an internal web service into my Spring beans.xml dynamically, using a PropertyPlaceHolderConfigurer.
Here's the scenario:
My web application is deployed in WebLogic 10.3.
The WSDL url is contained in a properties file that is located outside my application (directly under the corresponding domain folder, while my application is inside the autodeploy folder). I set the location of this properties file in my domain's setDomainEnv.cmd file like below:
set JAVA_PROPERTIES=%JAVA_PROPERTIES% %CLUSTER_PROPERTIES% -Dproperty.file.path.config=%DOMAIN_HOME%\Service.properties
This is what my Service.properties file contains:
Service.WSDL.PATH=http://localhost:8088/mockServiceSoap?WSDL
My Spring beans.xml configuration:----
<bean id="file.path" class="java.lang.System" factory-method="getProperty">
<constructor-arg index="0"><value>property.file.path.config</value></constructor-arg>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" ref="file.path"/>
</bean>
<bean id="myServiceId" class="com.test.service.ServiceImpl">
<property name="myServiceSoap">
<ref bean="myService"/>
</property>
</bean>
<bean id="myService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="serviceInterface" value="com.test.service.ServiceSoap"/>
<property name="wsdlDocumentUrl" value="${Service.WSDL.PATH}"/>
</bean>
I enabled DEBUG log specifically for PPC and this is what I saw in my application log:
INFO org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 178 - Loading properties file from URL [file:D:/bea10.3/user_projects/domains/my_domain/Service.properties]
So, it seems that although the Service.properties file is getting loaded by PPC, the ${Service.WSDL.PATH} is NOT getting replaced.
What am I doing wrong here?
Also, how can I find out if PPC tried replacing the value of the placeholder and with what value? I was hoping the log file would contain that info but there was nothing there.
Any help is appreciated.
I've figured out, that PropertyPlaceholderConfigurer needs to be declared first in the application Context file, otherwise there's no guarantee of the load order. It took me a few hours to realize this.
Try moving the "file.path" bean into the PropertyPlaceHolderConfigurer's location property.

Can't get PropertyPlaceholderConfigurer to work the way I want

I'm trying to use PropertyPlaceholderConfigurer to read my app configuration. Basically there is a first configuration file in WEB-INF/config/config.properties that contains the filesystem location of another properties file (so that it is not destroyed during app deploy or update). I'm trying to have this set up in servlet-context.xml but only the first one works:
<bean id="propertyConfigurerInternal"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/config/config.properties</value>
</property>
</bean>
<bean id="propertyConfigurerExternal"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
depends-on="propertyConfigurerInternal">
<property name="location">
<value>file:${baseDataFolder}/jaccise.conf</value>
</property>
</bean>
The first one (propertyConfigurerInternal) is created with no problem, but the second one fails like this:
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: ${baseDataFolder}\jaccise.conf (Impossibile trovare il percorso specificato) <- Means "Can't find file in specified path"
like the EL expression is not working... How do I solve this?
You could do this:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/config/config.properties</value>
<value>file:${baseDataFolder}/jaccise.conf</value>
</list>
</property>
</bean>
Just use a single PlaceholderConfigurer and see if that is the cause of your problem. I've never seen two instances being created like you're attempting and I'm guessing that this can be problematic.
Hope this helps.
depends-on="propertyConfigurerInternal" means that propertyConfigurerInternal is initialized before propertyConfigurerExternal.
And ${baseDataFolder} must be included in the property files of propertyConfigurerInternal (in your case: config.properties), otherwise it cannot be parsed.
So, you should have a look at your /WEB-INF/config/config.properties to see whether ${baseDataFolder} was set within the file.

spring configuration with system property

I have a question. Currently i start jboss with -P property that links to file with properties.
In this property file i have property - mongo.server.list=127.0.0.1. In Spring configuration i try to set this property as value of constructor of bean. But spring treat ${mongo.server.list} as value itself.
Here is the code
<bean id="systemPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
</bean>
<bean id="mongo" class="com.mongodb.Mongo">
<constructor-arg index="0">
<value>${mongo.server.list}</value>
</constructor-arg>
</bean>
You should set your property file as a JVM property and read it in spring mvc configuration file as:
<context:property-placeholder location="file:///${-P}" />
You should specify the property as a JVM arg as -Dmongo.server.list=....
Did you try to start your server using -Dmongo.server.list=127.0.0.1 ? (i.e. using -D instead of -P, and no space between -D and the property name)

Resources