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 {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.getWriter().print("hello word");
resp.getWriter().flush();
resp.getWriter().close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.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 {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
System.out.println("doFilter");
arg2.doFilter(arg0, arg1);
}
@Override
public 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 {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("listener destroy");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("listener init");
}
}
3、在SpringBoot的启动类中,添加如下Bean配置,启动类如下
扫描二维码关注公众号,回复:
2238178 查看本文章
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;
@SpringBootApplication
public 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,查看控制台的打印信息,确认是否注册完成。