Contextloaderlistener or Not

ContextLoaderListener or not?

In your case, no, there's no reason to keep the ContextLoaderListener and applicationContext.xml. If your app works fine with just the servlet's context, that stick with that, it's simpler.

Yes, the generally-encouraged pattern is to keep non-web stuff in the webapp-level context, but it's nothing more than a weak convention.

The only compelling reasons to use the webapp-level context are:

  • If you have multiple DispatcherServlet that need to share services
  • If you have legacy/non-Spring servlets that need access to Spring-wired services
  • If you have servlet filters that hook into the webapp-level context (e.g. Spring Security's DelegatingFilterProxy, OpenEntityManagerInViewFilter, etc)

None of these apply to you, so the extra complexity is unwarranted.

Just be careful when adding background tasks to the servlet's context, like scheduled tasks, JMS connections, etc. If you forget to add <load-on-startup> to your web.xml, then these tasks won't be started until the first access of the servlet.

ContextLoaderListener not found

This should have nothing to do with your dependencies resolution at compile time since you can compile your project and the configuration seems quite fine in your project descriptor.

However, as you are receiving a set of ClassNotFoundException , that means that the application server is not able to find some classes at runtime, those basically related to Spring, so you have to review your project artifact dependencies.

Within Intellij IDEA:

  • Open the project configuration view: File -> Project Structure
  • Navigate to your project web artifact: Project Settings -> Artifacts then click on your project exploded war artifact.
  • In the Output Layout tab, navigate to the libraries folder, i.e. /WEB-INF/lib/ and check that all the Spring needed artifacts are included. In case any one is missing, which should be the case for the core, web and mvc dependencies I guess.

How is ContextLoaderListener from Spring being run even when web.xml does not include it?

Servlet 3.0 introduced ServletContainerInitializer

Interface which allows a library/runtime to be notified of a web
application's startup phase and perform any required programmatic
registration of servlets, filters, and listeners in response to it.

If you have the spring-web jar on your classpath, it implicitly registers its own implementation of this interface, SpringServletContainerInitializer. This in turn scans for implementations of WebApplicationInitializer on the classpath.

You apparently have SpringWebApplicationInitializer which

[...] initializes Spring context by adding a Spring
ContextLoaderListener to the ServletContext.

The ContextLoaderListener you see most likely comes from that.

Role/Purpose of ContextLoaderListener in Spring?

Your understanding is correct. The ApplicationContext is where your Spring beans live. The purpose of the ContextLoaderListener is two-fold:

  1. to tie the lifecycle of the ApplicationContext to the lifecycle of the ServletContext and

  2. to automate the creation of the ApplicationContext, so you don't have to write explicit code to do create it - it's a convenience function.

Another convenient thing about the ContextLoaderListener is that it creates a WebApplicationContext and provides access to the ServletContext via ServletContextAware beans and the getServletContext method.

ContextLoaderListener problem while spring is loading up

Sorry guys, it was due to a simple problem in tomcat.

Long back I was working on a project which was not deleted from tomcat webapp folder. That was spring project too.

I figured it out by commenting out all in web.xml in the current project I am working on. When I did so, it was still trying to load spring container. That's when I opened tomcat manager web application and squinted for the application that wasn't bootstrapped.

I found an application that was not bootstrapped in tomcat manager web application, then I deleted that application, now my application started working like charm...

Confused about using ContextLoaderListener in Spring MVC

the xml files specified above are located on the root of the classpath. i.e. WEB-INF/classes. See here for more details

Difference between ContextLoaderListener and ContextLoaderServlet

The javadoc for ContextLoaderServlet says it all:

Note that this class has been deprecated for containers implementing Servlet API 2.4 or higher, in favor of ContextLoaderListener.

Apparently prior to Servlet API 2.4 the order in which listeners versus servlets are initialized is not mandated by the specification. So to ensure that the Spring context is correctly loaded before any other servlets in a Servlet 2.3 and lower container, you would need to use ContextLoaderServlet and put it as the first to load on startup. Check out that link for further details.

class org.springframework.web.context.ContextLoaderListener not found

This is because you mark your Spring dependencies with <scope>provided</scope> on your maven pom.xml and you did not include the actual jars on the classpath when running the program. There are two possible solutions:

  1. Change the dependency scope into compile
  2. Include the spring jars with matching version to your classpath when running the application


Related Topics



Leave a reply



Submit