Custom log4j.properties location for Spring web application - spring

Is it possible to provide custom location for the log4j.properties file for the log4j 1.x?
I don't want to include the file in the project's resources folder.

configure the org.springframework.web.util.Log4jConfigListener in your web.xml with context-parameter log4jConfigLocation.
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>file:C:/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
Log4jConfigListener JavaDoc:
Bootstrap listener for custom log4j initialization in a web environment. Delegates to Log4jWebConfigurer...
Log4jWebConfigurer JavaDoc:
Location of the log4j config file; either a "classpath:" location
(e.g. "classpath:myLog4j.properties"), an absolute file URL (e.g.
"file:C:/log4j.properties), or a plain path relative to the web
application root directory (e.g. "/WEB-INF/log4j.properties"). If not
specified, default log4j initialization will apply ("log4j.properties"
or "log4j.xml" in the class path; see the log4j documentation for
details). "log4jRefreshInterval":

Related

Spring container is looking at wrong path while loading multiple spring configuration files

We are using Spring framework in the project. I am loading applicationContext file from web.xml in the following way:
<servlet>
<servlet-name>SpringDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/CustomerService/applicationContext.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDispatcherServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
I am trying to load one configuration file JobDesigner-springintegration.xml inside the applicationContext file in the following way:
<import resource="JobDesigner-springintegration.xml"/>
Now, I am trying to load few more configuration files inside the
`JobDesigner-springintegration.xml` file in the following manner:
<import resource="*.xml"/>
But weirdly the spring container is looking in a wrong path something like below:
jndi:/localhost/cciupg/WEB-INF/CustomerService/
The log excerpts is shown below:
12:52:15,202 INFO [STDOUT] 12:52:15,202 INFO [XmlBeanDefinitionReader] Loading XML bean definitions from ServletContext resource [/WEB-INF/CustomerService/JobDesigner-springintegration.xml]
12:52:15,453 INFO [STDOUT] 12:52:15,448 WARN [PathMatchingResourcePatternResolver] Cannot search for matching files underneath URL [jndi:/localhost/cciupg/WEB-INF/CustomerService/] because it does not correspond to a directory in the file system
java.io.FileNotFoundException: URL [jndi:/localhost/cciupg/WEB-INF/CustomerService/] cannot be resolved to absolute file path because it does not reside in the file system: jndi:/localhost/cciupg/WEB-INF/CustomerService/
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:205)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at org.springframework.core.io.UrlResource.getFile(UrlResource.java:169)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.doFindPathMatchingFileResources(PathMatchingResourcePatternResolver.java:526)
at org.springframework.web.context.support.ServletContextResourcePatternResolver.doFindPathMatchingFileResources(ServletContextResourcePatternResolver.java:92)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:347)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:279)
at org.springframework.context.support.AbstractApplicationContext.getResources(AbstractApplicationContext.java:1269)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:208)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:261)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:197)
at org.springframework.beans.factory.xml.DefaultBeanDe
The question here is why the spring container is looking at wrong path jndi:/localhost/cciupg/WEB-INF/CustomerService/ instead of /WEB-INF/CustomerService/?
And the weird part is why it is appending jndi:/localhost/cciupg to the path?
WEB-INF is not part of the classpath I believe. Therefore it cannot find the your *.xml files. I believe jndi is likely the last location spring has checked when throwing the error after looking down the classpath.
In other words, try putting your files in the classpath. In java projects I usually see resources in the main -> resources path.

(Spring) class path resource exception java.io.FileNotFoundException

I have aded following code to my web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:com/neelamhotel/mavenwebproject5/configs/dao-context.xml
</param-value>
But I am getting error for the same as
IOException parsing XML document from class path resource [com/neelamhotel/mavenwebproject5/configs/dao-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/neelamhotel/mavenwebproject5/configs/dao-context.xml] cannot be opened because it does not exist
Project Structure
Explorer View
I know dao-context.xml file is there. But then why it is giving an error ?
Updated Explorer view
Created WAR file view
In a Maven project, src/main/java is for Java source files. The other files are ignored by Maven.
Resources (i.e. non-Java files) that must be copied and be available in the classpath must go in src/main/resources.
Gradle uses the same conventions.

Deploying a camel project having no main method,web.xml

My project consists of a spring xml(having the camel context, weblogic configuration parameters, jms related configurations) a pojo having the route for camel (whose reference we provide in spring) and an xsl.
My project has to listen on a queue, transform the message and publish it onto another queue. Everything works fine until i try to deploy it. How do i go about deploying it...i do not have a main java class...i tested my code by invoking Main of org.apache.camel.spring.Main.
I have to deploy it onto weblogic since my route consumes from weblogic queue seamlessly. How do i do it?.jar or .war or .ear? And how do i go about creating my .jar or .war or .ear? Remember i have only 3 files in my project structure and a few dependent jars
You will deploy it as a war file. I have not done this with Weblogic but the steps for Tomcat are listed below:
First make sure that all the needed jars are packaged in your war file.
Bootsrap Spring to your WAR file by adding the following to your web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
Add location of your Spring XML file by using the following:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/camel-context.xml</param-value>
</context-param>
Package the war with Maven i.e. run the command mvn package
Deploy the war file to the server.

serve static resource using spring 3.0.0 and tomcat

I need to expose some static content (xsd files that are required by the wsdl). I cannot use the mvn:resources since it is not available in Spring 3.0.0
I don't have an idea as to where the static content should go. Hope someone can help me.
In my web.xml i have
<servlet>
<servlet-name>Resources</servlet-name>
<servlet-class>org.springframework.web.servlet.ResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resources</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
then in the webapp dir I added a resources dir with the static files.
% ls webapp
index.jsp META-INF resources WEB-INF
can someone tell me where the static content should go.
thanks much.
Dont forget this one aswell:
<!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource
requests to the container's default Servlet -->
<mvc:default-servlet-handler/>
You wrote
I cannot use the mvn:resources since it is not available in Spring 3.0.0
This is complete wrong!
mvn:resources is avaiable in Spring mvc namespace for version 3.0.0 3.0.4
See the xsd: http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

how to change application context path for ContextLoaderListener

In my application by default org.springframework.web.context.ContextLoaderListener looks for Applicationcontext.xml in WEB-INF folder. Now i want to move Applicationcontext.xml file to src/main/resources folder. But if i do this i get error java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml].
How do i specify ContextLoaderListener to look for file in resources folder? please help
If you added it to Mavens resources folder simply add:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
to your web.xml.

Resources