Sunday, October 16, 2016

Make ServletContextListener spring aware

Leave a Comment

I am plugging in Spring to existing Java EE web Application. I have following lines in my web.xml:

<listener>     <listener-class>com.MyContextListener</listener-class> </listener>  

And Following MyContextListener class?

public class MyContextListener implements ServletContextListener {      public void contextInitialized(ServletContextEvent event) {        //...     } } 

What should I do to make MyContextListener be managed by Spring?


Edited:

My assumption is: Spring should create all servlets and all web app infrastructure so everything happened in contextInitialized method of MyContextListener should be somehow handled by Spring. How can I achieve, by implementing some interface I suppose. Correct me if I am wrong. Thanks!

4 Answers

Answers 1

What should I do to make MyContextListener be managed by Spring?

It depends on which configuration way you are using. Anyway, you should tell directly Spring to use the class you have declared. That could be done by the following way:

@WebListener public class MyContextListener implements ServletContextListener { ... } 

A class marked with this annotation (the Servlet 3.0 specification, 8.1.4) must implement one of these interfaces

HttpSessionAttributeListener HttpSessionListener ServletContextAttributeListener ServletContextListener (+) ServletRequestAttributeListener ServletRequestListener HttpSessionIdListener 

that it actually does.

Personally, I prefer a meta-annotation based approach which makes my configuration shorter and more concise.

Spring should create all servlets and all web app infrastructure so everything happened in contextInitialized method of MyContextListener should be somehow handled by Spring.

Yes, Spring will do it for you if you provide some information which could help it to register / configure / create / manage an instance.

The information may be either meta-information (a template that tells how to create an instance, like BeanDefinitions) or a completed instance itself (usually, it gets passed programmatically that, in turn, leads to writing a huge amount of code).

How can I achieve, by implementing some interface I suppose.

You are implementing an interface to make your listener a listener (a class that describes specific methods which will be called at some points of time). Spring, itself, is responsible for guaranteeing such calls at those points of time, placing an object in the existing web infrastructure before.

Answers 2

Annotate where you create a new instance of MyContextListener with @Bean if using Java Configs with Spring Boot.

Answers 3

Either annotate the class with @WebListener or the method with @Bean

Answers 4

Well,

We had a similar scenario of configuring an exiting Jersey web services app to use Spring for dependency injection. Our Jersey webapp had extended ContextLoaderListener as follow

public class XServletContextListener extends ContextLoaderListener {     ...      @Override     public void contextInitialized(ServletContextEvent arg0) {         super.contextInitialized(arg0);         ....     }      @Override     public void contextDestroyed(ServletContextEvent arg0) {         super.contextDestroyed(arg0);         ....     } }  

where ContextLoaderListener is

import org.springframework.web.context.ContextLoaderListener; 

We included the jersey-spring bridge with all spring dependencies including applicationContext.xml as follow

<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd     http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-4.0.xsd">      <context:component-scan base-package="com.xxx.*" />     ....     .... </beans> 

And obviously needed to make sure that XServletContextListener is included in the web.xml as follow

<context-param>     <param-name>contextConfigLocation</param-name>     <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener>     <listener-class>com.xxx.**.XServletContextListener</listener-class> </listener> 

Followed by servlet and its init-param values and servlet mapping. You can obviously adopt annotation config in place of xml confib in which case you would need to use WebListener annotation.

We use a variety of annotations such as

@Component for objects @Service for services  @Repository for DAOs @Controller for controllers/resources  @ContextConfiguration for tests 

Everything is loaded and autowired by Spring framework.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment