Four registration postures of Listener in spring boot series

Keywords: Programming Spring Java xml Maven

Filter and servlet, the three elements of java web, are introduced respectively. Next, let's take a look at the related knowledge points of Listener. The main content of this blog is how to customize the Listener and register it to the spring container in the spring boot environment

<!-- more -->

I. environment configuration

1. Project construction

First of all, we need to build a web project to facilitate the demonstration of the following servelt registration examples. You can create a project through the spring boot official website, or you can build a maven project. In pom.xml, the configuration is as follows

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/libs-snapshot-local</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/libs-milestone-local</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release-local</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

II. Listener registration

The Listener we mentioned here refers to the Listener related to java web, which is different from the Listener of Spring itself. In java web, the knowledge point of Listener is the set of servlet specifications, which will not be expanded in detail here. Here are four ways to use Servlet Listener in Spring boot

1. WebListener annotation

@WebListener annotation is the annotation provided by Servlet3 +, which can identify a class as Listener. The usage gesture is not too different from the previous Listener and Filter

@WebListener
public class AnoContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("@WebListener context Initialization");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("@WebListener context Destruction");
    }
}

Because WebListener annotation is not the specification of spring, in order to identify it, you need to add annotation @ ServletComponentScan on the startup class

@ServletComponentScan
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

2. Ordinary bean

The second way is to use the Listener as a normal spring bean, and spring boot will automatically wrap it as a ServletListenerRegistrationBean object

@Component
public class BeanContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("bean context Initialization");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("bean context Destruction");
    }
}

3. ServletListenerRegistrationBean

Use java config to actively insert a common Listener object into the ServletListenerRegistrationBean object and create it as a spring bean object

public class ConfigContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("config context Initialization");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("java context Destruction");
    }
}

The above is just a common class definition, and the following bean creation is the key point

@Bean
public ServletListenerRegistrationBean configContextListener() {
    ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean();
    bean.setListener(new ConfigContextListener());
    return bean;
}

4. ServletContextInitializer

This is mainly to actively add Filter, Servlet and Listener to the ServletContext context, so as to achieve an active registration effect

public class SelfContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("ServletContextInitializer context Initialization");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("ServletContextInitializer context Destruction");
    }
}

@Component
public class ExtendServletConfigInitializer implements ServletContextInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.addListener(SelfContextListener.class);
    }
}

Note the active registration time of ExtendServletConfigInitializer. The listener is added at startup, so its priority will be the highest

5. test

Four registration methods are introduced above, all of which can take effect. In our actual development, we can choose one on demand, and it is not recommended to mix multiple methods;

After the project is started and shut down, the output log is as follows

II. other

web series blog

Project source code

1. A grey Blog

The best letter is not as good as the above. It's just a one-of-a-kind remark. Due to the limited personal ability, there are inevitably omissions and mistakes. If you find a bug or have better suggestions, you are welcome to criticize and correct. Thank you very much

Here is a grey personal blog, recording all the blogs in study and work. Welcome to visit

Posted by madcat on Thu, 12 Dec 2019 22:01:42 -0800