How to read from property file in web.xml - spring

in my web.xml i want to read the welcome file from property file
something like:
<welcome-file-list>
<welcome-file>${home.page}</welcome-file>
</welcome-file-list>
i have propertyPlaceholderConfigurer configured:
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:messages/application.properties</value>
</list>
</property>
</bean>
is there's additional param should be added to web.xml, or another bean needs to be defined or what ?
also i have another xml file on the same level of web.xml (under WEB-INF direclty)
can i read from property file in it in the same way ?
please advise.

It doesn't work like that; the web.xml file is completely unrelated to Spring.
What you could do is have a hard-coded welcome file, and inside that file, redirect to something defined in the Spring configuration, retrieving the page by grabbing the Spring context manually.

Related

Use values from properties file in web.xml

I am a begginer in Spring and encountered the following problem.
I have the following files:
app.properties
applicationContext.xml
web.xml
Within app.properties I have the value app.env=dev
In applicationContext.xml:
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="location">
<value>app.properties</value>
</property>
</bean>
I want to use the app.env from the properties file in web.xml like this:
<context-param>
<param-name>ENV</param-name>
<param-value>${app.env}</param-value>
</context-param>
I am using ${app.env} without problems in applicationContext.xml.
I have found discussions that state you can push values into web.xml from a properties file but I have also found people saying it is not possible (usually older posts).
The issue is the placeholder ${app.env} in web.xml is not replaced by the value from the properties file. Is it possible to do it? If so what am I missing.

Loading log4j.xml from outside of war file

For my spring application, I have a requirement to move log4j.xml outside of war file. I have been searching for solutions, and found following ways:
org.springframework.web.util.Log4jConfigListener: this did not work as war is not expanded on tomcat.
Passing jvm property (-Dlog.Localtion=....): Since log4j file is application specific, I do not think this is good way to do this.
I am wondering what is best way to solve this. I believe Spring should make it easy someway, it's just I don't much about it.
You can specify the location of your log4j.xml in your context config file:
<bean id="log4jInitialization"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass"
value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>/any/path/log4j.xml</value>
</list>
</property>
</bean>
Source http://helpdesk.objects.com.au/java/how-to-specify-log4j-configuration-in-spring-application
You can check org.springframework.util.Log4jConfigurer. You can pass the location of log4j.xml to this class.It also takes care of resolving property place holders.
You can use org.springframework.web.util.Log4jConfigListener in your web.xml - just add this elements:
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>../path/to/config</param-value>
</context-param>

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

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.

Spring :Why can't configure two ServletContextPropertyPlaceholderConfigurer in separated .xml?

Greetings,
I am working on a spring based web application. Situation is :
there are two .xml file one application-context.xml , other is default-context.xml
application-context.xml will be load to web context when tomcat started ,which is configured in web.xml
application-context.xml ServletContextPropertyPlaceholderConfigurer loaded some properties.and import default-context.xml
because default-context.xml is in another project, and I want it have its own .properties(default-context.properties) file and set up a ServletContextPropertyPlaceholderConfigurer for it to load the properties.
the current result is that : the properties in default-context.properties is not loaded, and the ServletContextPropertyPlaceholderConfigurer in default-context.xml is not inited. It reports
can't resolve placeholder for xxxxxx"
I tried some combinations,
1.to load default-context.properties in application-context.xml 's ServletContextPropertyPlaceholderConfigurer ,its worked.
2.to load default-context.properties in application-context.xml 's PropertyPlaceholderConfigurer ,NOT work. I guess its because the PropertyPlaceholderConfigurer can't be load to servlet context?
3.loading default-context.properties in default-context.xml in both ways (ServletContextPropertyPlaceholderConfigurer or PropertyPlaceholderConfigurer)
NOT work.
I can't figure out why there's only one ServletContextPropertyPlaceholderConfigurer can be configured in servlet context?
configuration is like :
in application-context.xml
<class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:etc/system.properties
</value>
...
in default-context.xml :
<bean id="tempName123"
class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer" >
<property name="locations">
<list>
<value>classpath:etc/default-datasource.properties</value>
</list>
</property>
</bean>
You can use different placeholder prefix and suffix for second bean. With following declaration you can use placeholder as #[some.property.name].
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"
p:location="classpath:etc/default-datasource.properties"
p:placeholderPrefix="#["
p:placeholderSuffix="]">
</bean>

Resources