Spring PropertyPlaceholderConfigurer not replacing placeholder - spring

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.

Related

Could not resolve placeholder in string value with external files

I have configured a config.xml file choosing the appropriate properties file depending on environment. I am running this as a Spring Boot app with Apache Camel.
The config looks like this.
<bean id="properties"
class="org.apache.camel.component.properties.PropertiesComponent">
<property name="locations" ref="locations" />
</bean>
<bean id="bridgePropertyPlaceholder"
class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
<property name="locations" ref="locations" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<beans profile="dev">
<util:list id="locations">
<value>classpath:config/users.properties</value>
<value>classpath:config/application.properties</value>
<value>classpath:config/application-dev.properties</value>
</util:list>
<beans profile="test">
<util:list id="locations">
<value>file:${project.dir}/config/users.properties</value>
<value>file:${project.dir}/config/application.properties</value>
</util:list>
When using the test profile I want to use the external files defined in the config (because I dont want to commit username/password to repo). That seems to work okay.
However, my users.properties file contains:
username=myusername
password=mypassword
and my application.properties contains:
loginParameters=username=${username}&password=${password}
when running java -jar myjar.jar --spring.profiles.active=test I encounter:
java.lang.IllegalArgumentException: Could not resolve placeholder 'username' in string value '${username}&password=${password}'
It clearly loads the properties files because it states:
Loading properties file from URL: file:...../users.properties
Loading properties file from URL: file:...../application.properties
Bridging Camel and Spring property placeholder configurer with id: bridgePropertyPlaceholder
...
And then the exception occurs. How can I resolve the issue where the application.properties file doesn't recognize my properties defined in users.properties? Everything works okay when running the dev-profile.
in your application properties, use $simple{username} and $simple{password} to tell Camel to put the values there.

Weblogic Spring Dependency Injection from flat-files on the file-system

I have an application deployed into Oracle Weblogic 10.3. In my .ear files, I use spring injection from xml files in the ear files. I would like to also perform dependency injection from xml files placed on the server file-system. We do something similar to this in Karaf, where we place our configuration files for bundles in the conf directory in the server itself. Is there a similar way to do this in weblogic?
You can ask spring to read properties from file systems as below;
<!-- Reads application properties and uses them in the application context -->
<bean 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>file:D:\somefolder\application.properties</value>
<value>classpath:com/foo/sp.properties</value>
</list>
</property>
</bean>

PropertyPlaceholderConfigurer does not find property file on disk

I am trying to move a working spring WAR to OSGI environment (in glassfish 3.1 and blueprint, spring 3.0.5).
The application loads properties file from disk, like this:
<bean id="myProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="${my_conf}/my.properties"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
</bean>
I see in debugger that ${my_conf}/my.properties is translated to the existing path (c:\conf\my.properties)
I use the property jms.url defined in my.properties in the next bean declaration
<amq:broker useJmx="false" persistent="false" brokerName="embeddedbroker">
<amq:transportConnectors>
<amq:transportConnector uri="tcp://${jms.url}"/>
<amq:transportConnector uri="vm://embeddedbroker" />
</amq:transportConnectors>
</amq:broker>
And in deployment I get an exception "Could not resolve placeholder ${jms.url}"
Why it fails? Is there another way to load properties from file on disk?
thank you
Since its an OSGI environment, you will need spring-osgi-core jar added to your application. Take a look at this link to configure property-placeholder for OSGI framework.
It isn't a solution, but an explanation of my problem.
The problem is related to this bug in spring 3 and osgi.
I had to open spring logs to debug level to understand it.

Spring application context external properties?

i have a Spring application and its working well so far. Now i want the properties file in an external config folder and not in the packed jar to change things without the need to repack. This is what i got:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- <property name="locations" value="classpath:/springcontext.properties"/> -->
<property name="locations" value ="config/springcontext.properties" />
The outcommented one is working and the other one i dont get to work :/ Can someone help?
Edit:
Thx 4 comments so far.
Maybe my question wasnt clear enough :). I perform a Maven build and everything will be packaged and i want this folder to be NOT in the package nut next to the outcomming jar and in this folder i want the properties file. possible?
You can try something like this:
<context:property-placeholder
location="${ext.properties.dir:classpath:}/servlet.properties" />
And define ext.properties.dir property in your application server / jvm, otherwise the default properties location "classpath:/" (i.e., classes dir of .jar or .war) would be used:
-Dext.properties.dir=file:/usr/local/etc/
BTW, very useful blog post.
You can use file prefix to load the external application context file some thing like this
<context:property-placeholder location="file:///C:/Applications/external/external.properties"/>
<context:property-placeholder location="classpath*:spring/*.properties" />
If you place it somewhere in the classpath in a directory named spring (change names/dirs accordingly), you can access with above
<property name="locations" value ="config/springcontext.properties" />
this will be pointing to web-inf/classes/config/springcontext.properties
This blog can help you. The trick is to use SpEL (spring expression language) to read the system properties like user.home, to read user home directory using SpEL you could use #{ systemProperties['user.home']} expression inside your bean elements. For example to access your properties file stored in your home directory you could use the following in your PropertyPlaceholderConfigurer, it worked for me.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>file:#{ systemProperties['user.home']}/ur_folder/settings.properties</value>
</property>
</bean>
This question is kind of old, but wanted to share something which worked for me. Hope it will be useful for people who are searching for some information accessing properties in an external location.
This is what has worked for me.
Property file contents:
PROVIDER_URL=t3://localhost:8003,localhost:8004
applicationContext.xml file contents: (Spring 3.2.3)
Note: ${user.home} is a system property from OS.
<context:property-placeholder system-properties-mode="OVERRIDE" location="file:${user.home}/myapp/latest/bin/my-env.properties"/>
<bean id="appsclusterJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
<prop key="java.naming.provider.url">${PROVIDER_URL}</prop>
</props>
</property>
</bean>
${PROVIDER_URL} got replaced with the value in the properties the file
One way to do it is to add your external config folder to the classpath of the java process. That's how I've often done it in the past.
<context:property-placeholder location="file:/apps/tomcat/ath/ath_conf/pcr.application.properties" />
This works for me.
Local development machine path is C:\apps\tomcat\ath\ath_conf and in server /apps/tomcat/ath/ath_conf
Both works for me

Weblogic classpath and Spring Resources

I'm trying to set a bean property like this:
<bean id="threadImport" class="com.foo.bat.util.ThreadImport" singleton="false">
<property name="mailSender" ref="mailSender"/>
<property name="parseConfFile" value="classpath:parse/import.xml" />
<property name="logFilename" value="/tmp/import.log" />
but none of files are found. What's the classpath for my deployed application? May I set it on any weblogic xml descriptors? Which is the best way to place and locate files used on spring applications?
I am using:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:foobar-config.properties" />
</bean>
The properties file is either location at the root of my test source folder so JUnit has a test-specific config available and in production, we have added a classpath entry to Weblogic pointing to the configuration folder. You can do that in the setDomainEnv.sh or for Managed Servers, in their configuration (web console), server start, classpath.

Resources