Loading log4j.xml from outside of war file - spring

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>

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?

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.

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)

Spring ReloadableResourceBundleMessageSource configuration

I develop a simple Spring application which is my university task. There are 3 configuration files: web.xml, core-context.xml, dispatcher-servlet.xml and 1 file with default properties which is called messages.properties and is located in /WEB-INF/ folder.
In my application I have the following configuration of ReloadableResourceBundleMessageSource and it works fine:
core-context.xml
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames" value="/WEB-INF/messages" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:core-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
But it turned out that my task instruction says that I should configure ReloadableResourceBundleMessageSource bean in dispatcher-servlet.xml. The problem is that whenever I remove the above configuration from core-context.xml and put it in dispatcher-servlet.xml my locals are no longer displayed.
Could you explain to me why the problem occurs?
What is a difference between putting bean configuration inside core-context.xml and dispatcher-servlet.xml?
You have not posted your dispatcher-servlet.xml but I believe it is not being initialized by the spring container because it is not declared anywhere. If you must have another file called dispatcher-servlet.xml as you specify then you can just import it in your core-context.xml. This should solve your problem.

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.

Resources