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
原创粉丝点击