SpringBoot學習3:springboot整合filter
阿新 • • 發佈:2019-02-04
cep att ted () eat bean string spring 路徑
整合方式一:通過註解掃描完成 Filter 組件的註冊
1、編寫filter
package com.bjsxt.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * Created by Administrator on 2019/2/4. */ @WebFilter(filterName = "FirstFilter",urlPatterns = {"/first"}) //filter名字和攔截的路徑 public class FirstFilter implementsFilter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("進入FirstFilter......."); filterChain.doFilter(servletRequest,servletResponse); System.out.println("離開FirstFilter......."); } @Override public void destroy() { } }
2、編寫啟動類
package com.bjsxt; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan; /** * Created by Administrator on 2019/2/4. */ @SpringBootApplication @ServletComponentScan //在springboot啟動時,會掃描@WebServlet、@WebFilter等,並將該類實例化 public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
整合方式二:通過方法完成 Filter 組件的註冊
1、編寫filter
package com.bjsxt.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * Created by Administrator on 2019/2/4. */ public class SecondFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("進入SecondFilter......."); filterChain.doFilter(servletRequest,servletResponse); System.out.println("離開SecondFilter......."); } @Override public void destroy() { } }
2、編寫啟動類
package com.bjsxt; import com.bjsxt.filter.SecondFilter; import com.bjsxt.servlet.SecondServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; /** * Created by Administrator on 2019/2/4. */ @SpringBootApplication public class App2 { public static void main(String[] args){ SpringApplication.run(App2.class,args); } /** * 註冊Servlet * @return */ @Bean public ServletRegistrationBean getServletRegistrationBean(){ ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet()); bean.addUrlMappings("/second"); return bean; } /** * 註冊Filter * @return */ @Bean public FilterRegistrationBean getFilterRegistrationBean(){ FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter()); bean.addUrlPatterns(new String[]{"/second"}); return bean; } }
SpringBoot學習3:springboot整合filter