Spring boot實現監聽器
package com.example.demo.listener.test;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* 使用@WebListener註解,實現ServletContextListener介面
* 在啟動器處加註解@ServletComponentScan
* @author cxx
*/
@WebListener
public class MyServletContextListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext初始化");
System.out.println(servletContextEvent.getServletContext().getServerInfo());
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext銷燬");
}
}
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@MapperScan(value = "com.example.demo.mapper")
//filter和listener監聽器註解需要用的
@ServletComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
複製程式碼
Spring Boot實現過濾器
第一步:定義MyFilter過濾器
package com.example.demo.filter.test;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* 使用註解過濾器
* @WebFilter將一個實現了import javax.servlet.Filter介面的類定義為過濾器
* 屬性filterName生命過濾器的名稱,可選
* 屬性urlPatterns指定要過濾的URL模式,也可使用屬性value來宣告(指定要過濾的URL是必選屬性)
*/
@WebFilter(filterName = "MyFilter",urlPatterns = "/*")
public class MyFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("過濾器初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("執行過濾操作");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.println("過濾器銷燬");
}
}
複製程式碼
第二部:在入口處新增@ServletComponentScan註解
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@MapperScan(value = "com.example.demo.mapper")
//filter和listener監聽器註解需要用的
@ServletComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
複製程式碼
Spring Boot全域性異常處理
全域性異常處理:@ControllerAdvice:包含@Componet,可以被掃描到
統一異常處理:@ExceptionHandler(Exception.class):用在方法上面表示遇到這個異常就執行以下方法。
package com.example.demo.global.exception.test;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ExceptionTestController {
@RequestMapping(value = "ExceptionTest",method = RequestMethod.GET)
public String index(){
String str=null;
str.split("1");
return "ExceptionTestController";
}
}
package com.example.demo.global.exception.test;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
@ControllerAdvice
//如果返回的為json資料或其他物件,新增該註解
@ResponseBody
public class GlobalDefaultExceptionHandler {
public static final String DEFAULT_ERROR_VIEW="error";
@ExceptionHandler({NullPointerException.class,NumberFormatException.class})
public ModelAndView formatErrorHandler(HttpServletRequest request,Exception ex){
System.out.println("已經捕獲到異常");
ModelAndView mv=new ModelAndView();
mv.addObject("exception",ex);
mv.addObject("timeStamp",new Date());
mv.setViewName(DEFAULT_ERROR_VIEW);
return mv;
}
}
複製程式碼