02.Spring Boot 实战~SpringBoot 整合Servlet,Filter, Listener
2020-07-15 05:16
337 查看
02.Spring Boot 实战~SpringBoot 整合Servlet,Filter, Listener
本文是上一篇文章的后续,详情点击该链接
SpringBoot 在 Controller 中常用注解
注解 | 说明 |
@RestController | @RestController 相当于@Controller+@ResponseBody 注解 如果使用@RestController 注解 Controller 中的方法无法返回页面,相当于在方法上面自 动 加 了@ResponseBody 注 解 , 所 以 没 办 法 跳 转 并 传 输 数 据 到 另 一 个 页 面 , 所以InternalResourceViewResolver 也不起作用,返回的内容就是 Return 里的内容 |
@GetMapping | @GetMapping 注解是@RequestMapping(method=RequestMethod.GET)的缩写 |
@PostMapping | @PostMapping 注解是@RequestMapping(method= RequestMethod.POST)的缩写 |
@PutMapping | @PutMapping 注解是@RequestMapping(method=RequestMethod.PUT)的缩写 |
@DeleteMapping | @DeleteMapping 注解是@RequestMapping(method=RequestMethod.DELETE)的缩写。 |
Spring boot整合Servlet
方式一:通过注解扫描完成 Servlet 组件的注册
创建Servlet
@WebServlet("/first") public class MyServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("hello world this is my first servlet"); } }
启动类
//启动类必须要有这个注解! @SpringBootApplication @ServletComponentScan// 在 spring Boot启 动 时 会 扫 描 @WebServlet public class SpringBootDemoApplication { public static void main(String[] args) { //必须要通过SpringApplication下的静态方法run //第一个参数是启动类的class,第二个参数是main方法里的args SpringApplication.run(SpringBootDemoApplication.class,args); } }
方法二:通过方法完成 Servlet 组件的注册
创建Servlet
这个时候我们就不再需要@WebServlet注解了
public class MyServlet2 extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("this is my second servlet"); } }
创建ServletConfig类
完成Servlet组件的注册
@Configuration public class ServletConfig { @Bean public ServletRegistrationBean getServletRegistrationBean(){ //实例化Servlet ServletRegistrationBean bean = new ServletRegistrationBean(new MyServlet2()); bean.addUrlMappings("/second"); return bean; } }
启动类
//启动类必须要有这个注解! @SpringBootApplication @ServletComponentScan// 在 spring Boot启 动 时 会 扫 描 @WebServlet public class SpringBootDemoApplication { public static void main(String[] args) { //必须要通过SpringApplication下的静态方法run //第一个参数是启动类的class,第二个参数是main方法里的args SpringApplication.run(SpringBootDemoApplication.class,args); } }
Spring boot 整合 Filter
方式一:通过注解扫描完成Filter组件注册
创建Filter
//拦截刚刚创建的MyServlet @WebFilter("/first") public class FirstFilter 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("start"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("end"); } @Override public void destroy() {} }
启动类
//启动类必须要有这个注解! @SpringBootApplication @ServletComponentScan// 在 spring Boot启 动 时 会 扫 描 @WebServlet,@WebFilter注解 public class SpringBootDemoApplication { public static void main(String[] args) { //必须要通过SpringApplication下的静态方法run //第一个参数是启动类的class,第二个参数是main方法里的args SpringApplication.run(SpringBootDemoApplication.class,args); } }
方式二:通过方法完成Filter组件注册
创建新的Filter
这个时候我们不再需要@WebFilter注解了
public class FirstFilter2 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("start"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("end"); } @Override public void destroy() {} }
完成Filter的注册
@Configuration public class FilterConfig { public FilterRegistrationBean getFilterRegistrationBean(){ FilterRegistrationBean bean = new FilterRegistrationBean(new FirstFilter2()); //拦截MyServlet bean.addUrlPatterns(new String[]{"/first"}); return bean; } }
启动类
//启动类必须要有这个注解! @SpringBootApplication @ServletComponentScan// 在 spring Boot启 动 时 会 扫 描 @WebServlet,@WebFilter注解 public class SpringBootDemoApplication { public static void main(String[] args) { //必须要通过SpringApplication下的静态方法run //第一个参数是启动类的class,第二个参数是main方法里的args SpringApplication.run(SpringBootDemoApplication.class,args); } }
Spring Boot 整合 Listener
方式一:还是使用注解
@WebListener public class FirstListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("Listener ....Init"); } @Override public void contextDestroyed(ServletContextEvent sce) { } }
启动类
//启动类必须要有这个注解! @SpringBootApplication @ServletComponentScan// 在 spring Boot启 动 时 会 扫 描 @WebServlet,@WebFilter注解,@WebListener public class SpringBootDemoApplication { public static void main(String[] args) { //必须要通过SpringApplication下的静态方法run //第一个参数是启动类的class,第二个参数是main方法里的args SpringApplication.run(SpringBootDemoApplication.class,args); } }
方式二:还是通过方法完成注册
配置类
@Configuration public class ListenerConfig { @Bean public ServletListenerRegistrationBean getServletListenerRegistrationBean(){ ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new FirstListener2()); return bean; } }
套路都一样,我也懒得再写了。。。
相关文章推荐
- SpringBoot 从零开始学习(二) 之整合servlet/filter/listener
- SpringBoot(四)-- 整合Servlet、Filter、Listener
- Spring Boot 学习(7)springboot 整合三大组件(Servlet、Filter、Listener)
- spring boot整合servlet、filter、Listener等组件方式
- spring boot(18)-servlet、filter、listener
- 在SpringBoot中使用 Servlet Filter Listener
- spring boot (二) servlet listener filter interceptor
- SpringBoot中配置Servlet,Filter,Listener的注解方式和Bean方式(十三)
- SpringBoot 配置 Servlet、Filter、Listener
- SpringBoot 配置 Servlet、Filter、Listener
- Spring Boot参考教程(六)Spring Boot配置Servlet,Filter,Listener,Interceptor
- springboot(4)添加Servlet、Listener、Filter
- 玩转Spring Boot 注册Servlet、Filter、Listener
- Spring Boot中Servlet&Filter&Listener&Interceptor的使用
- Spring Boot 编写Servlet、Filter、Listener、Interceptor的方法
- Spring Boot 之Servlet、Listener、Filter
- spring-boot如何定义一个servlet,filter,listener
- 在springboot中自定义Filter、Listener、Servlet、Interceptor
- SpringBoot 之Servlet、 Filter、Listener应用开发
- SpringBoot1.5.12.RELEASE注册Servlet三大组件【Servlet、Filter、Listener】