一:SpringBoot整合Servlet有两种方式
1.方式一:通过注解扫描完成Servlet组件的注册
1.1编写Servlet
@WebServlet(name="FirstServlet",urlPatterns="/first")
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
}
}
1.2编写启动类
/**
* SpringBoot 整合 Servlet 方式一
*
*
*/
@SpringBootApplication
@ServletComponentScan //在 springBoot 启动时会扫描@WebServlet,并将该类实例
化
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
该方式的重点在@ServletComponentScan
该注解在 springBoot 启动时会扫描@WebServlet,并将该类实例化。
2.方式二:通过方法完成Servlet组件的注册
2.1编写Servlet
public class SecondServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("SecondServlet..........");
}
}
2.2编写启动类
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean = new ServletRegistrationBean(new
SecondServlet());
bean.addUrlMappings("/second");
return bean;
}
}
该方法的重点在于springBoot的配置类:ServletRegistrationBean类的使用以及@bean注解
二:SptingBoot整合Filter有两种方式
1.通过注解扫描注册Filter组件
1.1编写Filter
/**
*SpringBoot 整合 Filter 方式一
*<filter>
* <filter-name>FirstFilter</filter-name>
* <filter-class>com.bjsxt.filter.FirstFilter</filter-class>
*</filter>
*<filter-mapping>
* <filter-name>FirstFilter</filter-name>
* <url-pattern>/first</url-pattern>
*</filter-mapping>
*/
//@WebFilter(filterName="FirstFilter",urlPatterns={"*.do","*.jsp"})
@WebFilter(filterName="FirstFilter",urlPatterns="/first")
public class FirstFilter 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("进入 Filter");
arg2.doFilter(arg0, arg1);
System.out.println("离开 Filter");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
1.2编写启动类
/**
*SpringBoot 整合 Filter 方式一
*
*/
@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
该方式的重点在@ServletComponentScan
该注解在 springBoot 启动时会扫描@WebFilter,并将该类实例化。
2.通过方法注册Filter组件
2.1编写Filter
/**
*
*SpringBoot 整合 Filter 方式二
*
*/
public class SecondFilter 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("进入 SecondFilter");
arg2.doFilter(arg0, arg1);
System.out.println("离开 SecondFilter");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
2.2编写启动类
/**
* SpringBoot 整合 Filter 方式二
*
*
*/
@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
*/
@Bean
public FilterRegistrationBean getFilterRegistrationBean(){
FilterRegistrationBean bean = new FilterRegistrationBean(new
SecondFilter());
//bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
bean.addUrlPatterns("/second");
return bean;
}
}
该方法的重点在于springBoot的配置类:FilterRegistrationBean类的使用以及@bean注解
三.SpringBoot整合Listener有两种方式
1.通过注解扫描的方式注册Listener组件
1.1编写Listener
/**
* springBoot 整合 Listener
*
*<listener>
* <listener-class>com.bjsxt.listener.FirstListener</listener-class>
*</listener>
*/
@WebListener
public class FirstListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("Listener...init......");
}
}
1.2编写启动类
/**
* springBoot 整合 Listener 方式一
*
*
*/
@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
该方式的重点在@ServletComponentScan
该注解在 springBoot 启动时会扫描@WebListener,并将该类实例化。
2.通过SpringBoot的注册类注册Listener
2.1编写Listener
/**
* springBoot 整合 Listener 方式二。
*
*
*/
public class SecondListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("SecondListener..init.....");
}
}
2.2编写启动类
/**
* SpringBoot 整合 Listener 方式二
*
*
*/
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
/**
* 注册 listener
*/
@Bean
public ServletListenerRegistrationBean<SecondListener>
getServletListenerRegistrationBean(){
ServletListenerRegistrationBean<SecondListener> bean= new
ServletListenerRegistrationBean<SecondListener>(new SecondListener());
return bean;
}
}
该方式的重点在于SpringBoot的配置类ServletListenerRegistrationBean的使用以及@Bean注解的使用