Spring boot filter, listener "Learn Spring boot from scratch"

Source: Internet
Author: User
Tags i18n log4j

Reprint: http://blog.csdn.net/linxingliang/article/details/52069490

The previous article has explained how the servlet is defined, and the method of registering filters (filter) and listeners (Listener) is just as unclear as the servlet, which can be viewed in the next article (20): This article will use @webfilter and @weblistener directly to complete a filter and a Listener; use annotations

@ServletComponentScan//This is to scan the corresponding servlet package;

Filters (Filter) file

Com.kfit.filter.MyFilter.Java

Package com.kfit.filter;

import java.io.IOException;

import Javax.servlet.Filter;

import Javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import Javax.servlet.annotation.WebFilter;

/**

*

* Use annotation Callout filter

* @WebFilter defines a class that implements the Javax.servlet.Filter interface as a filter

* Attribute filtername The name of the filter, optional

* attribute urlpatterns Specifies the URL pattern to filter, or it can be declared using the property value. (Specifies that the URL pattern to filter is a required attribute)

* @author Angel (qq:412887952)

* @version v.0.1

*/

@WebFilter (filtername= "Myfilter", urlpatterns= "/*")

Public class Myfilter implements filter{

@Override

Public void init (filterconfig config) throws servletexception {

System. out. println ("filter Initialization");

}

@Override

Public void DoFilter (servletrequest request, servletresponse response,

Filterchain chain) throws ioexception,servletexception {

System. out. println ("Perform filtering operations");

Chain.dofilter (request, response);

}

@Override

Public void destroy () {

System. out. println ("filter Destruction");

}

}

ServletContext Listener (Listener) file

Com.kfit.listener.MyServletContextListener:

Package Com.kfit.listener;

Importjavax.servlet.ServletContextEvent;

Importjavax.servlet.ServletContextListener;

Importjavax.servlet.annotation.WebListener;

/**

* Use @weblistener Annotations to implement Servletcontextlistener interface

*

* @author Angel (qq:412887952)

* @version v.0.1

*/

@WebListener

public class Myservletcontextlistenerimplements Servletcontextlistener {

@Override

Publicvoid contextdestroyed (servletcontextevent arg0) {

System.out.println ("Servletcontex destruction");

}

@Override

Publicvoid contextinitialized (servletcontextevent arg0) {

SYSTEM.OUT.PRINTLN ("Servletcontex initialization");

}

}

ServletContext Listener (Listener) file (Httpsessionlistener)

Myhttpsessionlistener.java

? package Com.kfit.listener;

Import Javax.servlet.annotation.WebListener;

Import javax.servlet.http.HttpSessionEvent;

Import Javax.servlet.http.HttpSessionListener;

/**

* Monitor session creation and destruction

*

*/

@WebListener

Publicclassmyhttpsessionlistenerimplementshttpsessionlistener {

@Override

Publicvoid sessioncreated (Httpsessioneventse) {

System.out.println ("Session is created");

}

@Override

Publicvoid sessiondestroyed (Httpsessioneventse) {

SYSTEM.OUT.PRINTLN ("Servletcontex initialization");

}

}

Be careful not to forget to add @servletcomponentscan annotations on Springbootsampleapplication.java.

During the startup process we will see the output:

Servletcontex initialization

Filter initialization

After the service starts, you can see the output by visiting a page:



"Spring Boot Series Blog"

Mybatic Insert Exception: Bindingexception:parameter ' name ' not found "learn spring B from zero"

Spring Boot write-back sense "start from scratch to learn Spring boot"

Spring Boot Validator Check "Learn Spring boot from scratch"

Spring boot internationalization (i18n) "Learn Spring boot from scratch"

Spring Custom Properties Upgrade Chapter "Learn Spring Boot from scratch"

In spring boot, use @async to implement the asynchronous call "Learn Spring boot from scratch"

Spring Boot service configuration and Deployment "Learn Spring boot from scratch"

Spring Boot Log Upgrade Chapter-logback "Learn Spring boot from scratch"

Spring Boot log Upgrade Chapter-LOG4J Multi-environment control of different log levels "Learn Spring boot from scratch"

The multi-environment configuration of Spring Boot properties File "Learn Spring boot from scratch"

Spring Boot Log Upgrade Chapter-log4j "Learn Spring boot from scratch"

Spring boot Log upgrade-theory "learn spring boot from scratch"

Spring Boot unit Test restfull API "Learn spring boot from scratch"

Spring Boot Send Message "Learn Spring boot from scratch"

The Web request log is uniformly processed using AOP in Spring boot

Spring boot MyBatis connect to MySQL database "Learn spring boot from scratch"

Spring Boot Logging slf4j "Learn spring boot from scratch"

Spring Boot Dynamic Data source (automatic switching of multiple data sources) "Learn Spring boot from scratch"

Spring boot multi-data source "Learn Spring boot from scratch"

Spring Boot uses Java code to create a bean and register it in spring "Learn Spring boot from scratch"

Springboot + devtools (Hot Deployment) "Learn Spring Boot from scratch"

39.4 Spring boot Shiro Rights Management "Learn spring boot from scratch"

39.3 Spring boot Shiro Rights Management "Learn spring boot from scratch"

39.2. Spring Boot Shiro Rights Management "Learn spring boot from scratch"

39.1 Spring boot Shiro Rights Management "Learn spring boot from scratch"

Spring Boot distributed session State save Redis "Learn spring boot from scratch"

The PNS Spring Boot integrated Ehcache implements the caching mechanism "learn Spring Boot from scratch"

Spring Boot cache theory "start from scratch with spring boot"

Spring Boot integrated REDIS implementation caching mechanism "Learn spring boot from scratch"

34Spring Boot Launcher starter detailed "Learn Spring boot from scratch"

Spring boot monitor and manage production environments "start from scratch with spring boot"

Spring boot uses the @springbootapplication annotation "Learn Spring boot from scratch"

More Viewing Blogs: http://412887952-qq-com.iteye.com/

"Spring Boot Series Blog"

Mybatic Insert Exception: Bindingexception:parameter ' name ' not found "learn spring B from zero"

Spring Boot write-back sense "start from scratch to learn Spring boot"

Spring Boot Validator Check "Learn Spring boot from scratch"

Spring boot internationalization (i18n) "Learn Spring boot from scratch"

Spring Custom Properties Upgrade Chapter "Learn Spring Boot from scratch"

In spring boot, use @async to implement the asynchronous call "Learn Spring boot from scratch"

Spring Boot service configuration and Deployment "Learn Spring boot from scratch"

Spring Boot Log Upgrade Chapter-logback "Learn Spring boot from scratch"

Spring Boot log Upgrade Chapter-LOG4J Multi-environment control of different log levels "Learn Spring boot from scratch"

The multi-environment configuration of Spring Boot properties File "Learn Spring boot from scratch"

Spring Boot Log Upgrade Chapter-log4j "Learn Spring boot from scratch"

Spring boot Log upgrade-theory "learn spring boot from scratch"

Spring Boot unit Test restfull API "Learn spring boot from scratch"

Spring Boot Send Message "Learn Spring boot from scratch"

The Web request log is uniformly processed using AOP in Spring boot

Spring boot MyBatis connect to MySQL database "Learn spring boot from scratch"

Spring Boot Logging slf4j "Learn spring boot from scratch"

Spring Boot Dynamic Data source (automatic switching of multiple data sources) "Learn Spring boot from scratch"

Spring boot multi-data source "Learn Spring boot from scratch"

Spring Boot uses Java code to create a bean and register it in spring "Learn Spring boot from scratch"

Springboot + devtools (Hot Deployment) "Learn Spring Boot from scratch"

39.4 Spring boot Shiro Rights Management "Learn spring boot from scratch"

39.3 Spring boot Shiro Rights Management "Learn spring boot from scratch"

39.2. Spring Boot Shiro Rights Management "Learn spring boot from scratch"

39.1 Spring boot Shiro Rights Management "Learn spring boot from scratch"

Spring Boot distributed session State save Redis "Learn spring boot from scratch"

The PNS Spring Boot integrated Ehcache implements the caching mechanism "learn Spring Boot from scratch"

Spring Boot cache theory "start from scratch with spring boot"

Spring Boot integrated REDIS implementation caching mechanism "Learn spring boot from scratch"

34Spring Boot Launcher starter detailed "Learn Spring boot from scratch"

Spring boot monitor and manage production environments "start from scratch with spring boot"

Spring boot uses the @springbootapplication annotation "Learn Spring boot from scratch"

More Viewing Blogs: http://412887952-qq-com.iteye.com/

Spring boot filter, listener "Learn Spring boot from scratch"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.