SpringBoot--添加配置Servlet,Filter,listener
来源:互联网 发布:java培训好不好 编辑:程序博客网 时间:2024/05/21 06:59
SpringBoot--添加配置Servlet,Filter,listener
SpringBoot中已经移除了web.xml文件,如果需要添加注册Servlet,Filter,Listener,则SpringBoot中有2种方式:
1、Servlet3.0api中的注解@WebServlet 、@WebListener、@WebFilter来配置
2、SpringBoot配置bean的方式进行配置
这里就详细介绍SpringBoot配置Bean的方式进行配置,
1.创建maven简单工程,加入SpringBoot框架
2.创建TestServlet,TestFilter,TestListener类,如下:
package com.supre.springboot.test;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class TestServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubresp.getWriter().print("hello word");resp.getWriter().flush();resp.getWriter().close();}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubthis.doGet(req, resp);}}
package com.supre.springboot.test;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;public class TestFilter implements Filter {@Overridepublic void destroy() {// TODO Auto-generated method stub}@Overridepublic void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {System.out.println("doFilter");arg2.doFilter(arg0, arg1);}@Overridepublic void init(FilterConfig arg0) throws ServletException {// TODO Auto-generated method stub}}
package com.supre.springboot.test;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class TestListener implements ServletContextListener {@Overridepublic void contextDestroyed(ServletContextEvent arg0) {// TODO Auto-generated method stubSystem.out.println("listener destroy");}@Overridepublic void contextInitialized(ServletContextEvent arg0) {// TODO Auto-generated method stubSystem.out.println("listener init");}}
3、在SpringBoot的启动类中,添加如下Bean配置,启动类如下
package com.supre.springboot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.embedded.FilterRegistrationBean;import org.springframework.boot.context.embedded.ServletListenerRegistrationBean;import org.springframework.boot.context.embedded.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import com.supre.springboot.test.TestListener;import com.supre.springboot.test.TestServlet;@SpringBootApplicationpublic class App{ public static void main( String[] args ){ System.out.println( "Hello World!" ); //ApplicationContext app = SpringApplication.run(App.class, args); //SpringContextUtil.setApplicationContext(app); SpringApplication.run(App.class, args); } @Bean public ServletRegistrationBean testServletRegistration() { ServletRegistrationBean registration = new ServletRegistrationBean(new TestServlet()); registration.addUrlMappings("/hello"); return registration; } @Bean public FilterRegistrationBean testFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(new TestFilter()); registration.addUrlPatterns("/"); return registration; } @Bean public ServletListenerRegistrationBean<TestListener> testListenerRegistration(){ ServletListenerRegistrationBean<TestListener> registration = new ServletListenerRegistrationBean<TestListener>(new TestListener()); return registration; }}
4、运行该SpringBoot启动类中的main方法,访问http://localhost:8080/hello,查看控制台的打印信息,确认是否注册完成。
0 0
- SpringBoot--添加配置Servlet,Filter,listener
- springBoot配置servlet,filter,listener
- springboot注册servlet,Filter,Listener
- SpringBoot初始教程之Servlet、Filter、Listener配置(七)
- SpringBoot初始教程之Servlet、Filter、Listener配置(七)
- SpringBoot初始教程之Servlet、Filter、Listener配置
- SpringBoot初始教程之Servlet、Filter、Listener配置
- SpringBoot中Servlet&Listener&Filter的应用
- 在SpringBoot中使用 Servlet Filter Listener
- springboot 使用Servlet,Filter,Listener的方法
- SpringBoot 实战 (二) :web 综合应用(配置Servlet、Listener、Filter,Log)
- springboot-11-servlet, listener, fitlter的添加
- 第四十二章 SpringBoot注册Servlet/Filter/Listener
- Servlet Filter Servlet Listener
- springboot mvc 配置servlet filter lister
- SpringBoot 里面配置servlet或filter
- springboot配置 java servlet的Filter过滤器
- SpringBoot系列<八>过滤器filter和监听器listener配置
- HttpServletRequest总结
- java视频压缩
- android_84_自定义属性
- PLSQL Developer插入中文乱码
- BZOJ3141: [Hnoi2013]旅行
- SpringBoot--添加配置Servlet,Filter,listener
- 向github提交代码
- [bsoj2258] 线性递推式
- JXL学习总结
- lvs-fullnat + keepalived
- linux下测试磁盘的读写IO速度
- CSS页面渲染优化属性will-change
- 软件需求规格说明书编写
- Unity3D 单利模式