Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ContextLoaderListener and ContextLoaderServlet

I know that getWebApplicationContext() finds the root WebApplicationContext through the ContextLoaderListener or ContextLoaderServlet

But I need to know the difference and when to use it?

like image 280
Some Java Guy Avatar asked Feb 07 '11 06:02

Some Java Guy


2 Answers

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.

like image 152
laz Avatar answered Nov 01 '22 02:11

laz


A context loader loads context configuration files ex (inside web.xml):

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
like image 2
Ariel U Avatar answered Nov 01 '22 01:11

Ariel U