ContextLoaderListener
is sort of bootstrapper to start up Spring's WebApplicationContext
while ApplicationListener
is more at the java application level itself rather than at the web application context.
ContextLoaderListener
is a great and standard tool for the contextualization of your app if it has multiple DispatcherServlet
s or some servlets/servlet filters mapped to different services. Basically, it is handy to have listeners for different servlets of such apps so that you can have fine-grained contextualization.
I do not know the nature of the application you are building but I assume that you are trying something basic for now. If this is the case, and even in cases where you have a more complex setup, it is better to load on startup a controller that takes care of your main initialization routines including your contextualization. You can use the controller bean you have with something like this in your web.xml:
<servlet>
<servlet-name>your-servlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
This servelt can also be mapped and invoked whenever you read a certain url pattern
E.g.
@RequestMapping("/welcome")
public ModelAndView helloWorld()
And in the web deployment descriptor, this bit is just like you map your servlets to other Spring services like Spring Security:
<servlet-mapping>
<servlet-name>crunchify</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
So you don't really need to use the constructor of a controller and neither do you always need to use ContextLoaderListener
or other listeners for simple initialization tasks. Nevertheless, it is handy to learn their use cases as you will need it when scaling your app.
Read more about dispatcher servlet here:
http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html