log4j with multiple applications in websphere - spring

I have two applications (war) deployed on a single websphere application server. Both applications have their own log4j properties and they are loaded from their respective web.xml as
From web.xml of App1
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:app1/app1Log4j.properties</param-value>
</context-param>
From web.xml of App2
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:app2/app2Log4j.properties</param-value>
</context-param>
Both applications create separate log files.
The problem I'm facing is When I restart the app server, log file for only one application is created. Log statements for 2nd application dont get written to any file. If I stop and start app2 again, then log files for app2 get created and logs are written for both the application.
I verified that the classloader policy at server level is "Multiple" and "parent last". Enabled the class loader service and verified that for both apps log4j.jar is loaded from WEB-INF/lib of corresponding wars. I even tried to set different "start order" for both apps, disabled "parallel start" at server level. But none of the options helped.
Both the log files get written when I start the applications individually after a delay, but one of them dont get written if I start them at the same time. Any help on how to overcome this problem is greatly appreciated.
Thanks,
SMan

Related

Spring boot JSF 2.0 application run as Jar

I have a spring boot+JSF application which runs as a charm when we run it as:
mvn spring-boot:run
However when i create a jar using mvn clean compile package install
and then run java -jar myapp.jar
i get:
java.lang.IllegalStateException: No Factories configured for this Application. This happens if the faces-initialization does not work at all - make sure that you properly include all configuration settings necessary for a basic faces application and that all the necessary libs are included. Also check the logging output of your web application and your container for any exceptions!
If you did that and find nothing, the mistake might be due to the fact that you use some special web-containers which do not support registering context-listeners via TLD files and a context listener is not setup in your web.xml.
A typical config looks like this;
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
in web xml as suggested here i tried below three options:
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
OR
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
OR
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
However the error remains the same, running out of ideas, appreciate any help on the same.
Best

spring 2.5 - Move /WEB-INF/applicationContext.xml to resources/META-INF

I've recently completed a spring mvc web application deployed as a war file built with maven that looked like this...
>MyApp
>src
>main
>java
>resources
>META-INF
>spring
>applicationContext.xml
>webapp
>WEB-INF
Now I'm trying to create a non-maven spring mvc application and I have this structure working...
>MyApp
>src
>WebContent
>WEB-INF
>applicationContext.xml
For this non-maven project I would like to move applicationContext.xml into a better location like I did in my maven project. I spent a lot of time playing around with it this morning but cannot find out how to do it successfully.
Can someone please give me a suggestion of how to best do it? i.e. If there should be a resources folder then where can I put it in my project structure and place the aplpicationContext.xml etc.
In my web.xml the location is specified as ....
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
Ideally after the change in location of my applicationContext.xml my web.xml would like similar to this.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:META-INF/spring/spring-application-context.xml
</param-value>
</context-param>
I am using spring 2.5 because I'm restricted by a very old version on websphere.
thanks

Eclipse Spring MVC Project Configuration Files

I have created a Spring MVC project through eclipse. I believe I used some plugins to generate the project directory. I find here there configuration files.
web.xml
root-context.xml
servlet-context.xml
I am kinda of familiar with Spring MVC & its dependency injection. However I have problems understanding the last two configuration files (root-context & servlet-context).
What kind of configurations do they contain?
Also in may online examples I see mvc-dispatcher-servlet.xml. Why did eclipse not generate this xml file in my project?
[IMPORTANT] I wanted to set up strong security and user authentication for my web app. I have been following online tutorials again and they all create a seperate
xml file named spring-security.xml and add the namespace information to that file. Does it suffice if I just create this file and add the name space information? I mean
dont' I need to import this file to a main file that is scanned by Spring framework?
How do I define and where do I put spring application context.xml file and start wiring the dependencies together? Also if I define everything (all dependencies here) how is this file picked up by the framework?
Thanks,
Configuration Files
If you check your web.xml you will find both of root-context.xml and servlet-context.xml files being referred here. One used by Dispatcher Servlet and other by Context Loader Listenter. You can name your files to whatever unless they are being refereed in web.xml
Eclipse Not generating files
Every editor works its own way. some may generate full fledged project/app with both DispatcherServlet and ContextLoaderListner configured or some with only DispatcherServlet ( with minimal configutaion). Check Spring Roo it starts with basic and gives you the flexibility to generate a strong app.
mvc-dispatcher-servlet.xml is not there
Some of the thing in spring projects are convention based, for example if you are not providing any file to your DispatcherServlet in web.xml spring looks for mvc-dispatcher-servlet.xml file, and if you have provided it won't look for.
Spring Security
To Configure Spring Security you need to provide at least some configuration. But the question is where. You need to add this configuration to your web.xml only. and Hence no need to import this to any other file.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener- class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/spring-security.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Where to define application context.xml
Just define it any where, configure beans in it.
You can add this file as follows:
a) Either Import this into some other configuration file like root-context.xml or servlet-context.xml
as <import resource="application-context.xml"/>
b) Add this into web.xml with ContextLoaderListner as context param
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/spring/application-context*.xml
classpath*:META-INF/spring/abc*.xml
</param-value>
</context-param>

Spring project not creating spring-config.xml

I have read some tutorials about using spring, and I've seen they speak about "spring-config.xml", but when I create a project I don't have that file, I have "application-config.xml", are they the same? Is the former the updated version of the latter? I am using Eclipse as IDE
The Spring Context only defines the concept of creating a Spring configuration where you will define spring components (beans, services, etc)
The XML itself can be named whatever you want, but in the web.xml file, you have to pass the xml name you choose to the spring context listener
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/thisXMLhaTheBestNameEver.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

load spring context

I use intelliJ Idea 10.5. when i use context-param for for spring context and run project Tomcat show me blank page!! when I disable it, page show!
<context-param>
<param-name>contextLocation</param-name>
<param-value>classpath:context/applicationContext-dao.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
You need to enable log4j and see the exception Spring is throwing when you start tomcat.. Also make sure that the context file is in the war file (on the location - WEB-INF/classes/context/applicationContext-dao.xml
Ian.

Resources