Use values from properties file in web.xml - spring

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.

Related

No appenders could be found for logger (org.springframework.web.context.ContextLoader)

I inherited a bunch of code and I noticed that in the tomcat logs it says
log4j:WARN No appenders could be found for logger
(org.springframework.web.context.ContextLoader). log4j:WARN Please
initialize the log4j system properly. log4j:WARN See
http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
The faq link mentions that this occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration.
How can I figure out how exactly to fix this. The xml file has the following. So I'm guessing its because there is no log4j.xml and instead there is one for each environment. Assuming that is the problem how do I configure things correctly.
<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>classpath:log4j-${environment}.properties</value>
</list>
</property>
</bean>
Few collective solutions -
While defining the bean, there shall be some property file from where the value(assume env) of ${environment} is being fetched. You must note that appending that value to the file name. Should exist under your project resources. In your case a file named log4j-env.properties.
Would recommend ways of using different method of logging for different environments while categorising them under different directory, so as to modify your values instead as -
<property name="arguments">
<list>
<value>#{environment + '/log4j.properties'}</value>
</list>
</property>
You could configure your Log4j listener in the web.xml instead of the spring-context.xml, so it is up before Spring starts. as -
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/${environment}/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
Source - Initializing Log4J with Spring?
Note - On how to assign the correct value to environment at runtime, follow How to replace a value in web.xml with a Maven property?

Java - Spring property file configuration for jar file

Java-Spring I have modules based project, i have module for DAO layer and module for business layer which is dependent upon DAO layer and web layer dependent upon DAO layer and business layer.
I am using maven for project compilation. and jar of every components are group under web projects lib folder.
Problem is i have spring context file and .property file inside DAO jar and following is my configuration but i spring unable to load properties i also tried prefixing value="classpath:abc.properties but it didn't work.
When i open the DAO jar both spring context and .properties files are on root.
<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="abc.properties" />
</bean>
<bean id="cmfModelDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="${jdbc.ConnectionUrl}"/>
<property name="username" value="${jdbc.Username}"/>
<property name="password" value="${jdbc.Password}"/>
</bean>
any idea how to quick fix this issue ?
I have a multi-module web project with Spring using the following code:
<context:property-placeholder location="classpath:env/env.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${env.datasource.driver}" />
<property name="url" value="${env.datasource.url}" />
<property name="username" value="${env.datasource.username}" />
<property name="password" value="${env.datasource.password}" />
</bean>
Don`t forget to verify the namespace url in the xml file:
xmlns:context="http://www.springframework.org/schema/context";
The folder env must be in classpath, so Spring can find it. My properties file is also inside a jar, and it`s working just fine.
I had that error and int might have to do with the way you are initializing the context, for example in my web app the problem was somehing with the filter I setup in the web.xml file. Also I end up using not an xml file but an Annotated Config Class and placed this in the web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.myapp.configuration.SpringConfig</param-value>
</context-param>
If you really want to use an xml file you must change the AnnotationConfigWebApplicationContext for an XmlWebApplicationContext. You should tell us how are you initilizing your context (like the code or web.xml if this is does not solve your issue)

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>

How to read from property file in web.xml

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.

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