SpringBoot series of tutorials web registration papers Listener four kinds of postures

java web three elements Filter, front Servlet were introduced, then we look at the Listener related knowledge, the main contents of this post for the next SpringBoot environment, and how to customize the Listener registered with the spring container

I. Environment Configuration

1. Project to build

First, we need to build a web project to facilitate the subsequent examples demonstrate servelt registered, you can create a project by spring boot official website, you can also create a maven project, the following configuration in pom.xml

<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

Here comes our Listener refers specifically related java web listener, and Listener Spring itself is not the same. Listener in java web in the knowledge that a point of the servlet specification, not to proceed here in detail. The following describes the four ways in SpringBoot in Servlet Listener

1. WebListener comment

@WebListenerAnnotated Servlet3 + annotation providers may be identified as a class The Listener, using a gesture and foregoing Listener, Filter and not much difference

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

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

Because the specification are not annotated WebListener spring, so in order to recognize it, it is necessary to add annotations based on starting@ServletComponentScan

@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 an ordinary spring bean, spring boot will automatically be packaged as ServletListenerRegistrationBeanan object

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

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

3. ServletListenerRegistrationBean

By java config to take the initiative to a common Listener object, into the ServletListenerRegistrationBeanobject, creating the bean object to spring

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

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

The above is just an ordinary class definition, the following bean creation is the key point

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

4. ServletContextInitializer

Here are the main means of actually ServletContext context creation, active Add Filter, Servlet, Listener to which, in order to achieve the effect of an active registered

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

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

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

Note ExtendServletConfigInitializer active registered opportunity, when you start adding this Listenrer, so it would be the highest priority

5. Test

Sign up above describes four ways, you can enter into force, in our actual development, you can choose a demand, do not recommend mixing a variety of ways;

After project startup and shutdown, the output log is as follows

II. Other

web series Bowen

Source Project

1. A gray Blog

Believe everything the book is not as good, above, is purely one of the words, due to limited personal capacity, it is inevitable omissions and mistakes, such as find a bug or have better suggestions are welcome criticism and generous gratitude

Here a gray personal blog, recording all study and work in the blog, welcome to go around

A gray blog

Guess you like

Origin www.cnblogs.com/yihuihui/p/12034522.html