springboot注册servlet组件有2种方式:
目录
1.通过注解直接注册
2.通过组件注册器进行注册
1.通过注解直接注册
一、启动类添加 @ServletComponentScan 可以扫描到servlet组件
package com.example._06servlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
/**
* @Package: com.example._06servlet
* @ClassName: AppConfig
* @Description: Description:
* @author: zq
* @since: 2020/5/5 17:33
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
@SpringBootApplication
@ServletComponentScan
public class AppConfig {
public static void main(String[] args) {
SpringApplication.run(AppConfig.class, args);
}
}
二、添加测试servlet类:TestServlet
package com.example._06servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
/**
* @Package: com.example._06servlet
* @ClassName: TestServlet
* @Description: Description:
* @author: zq
* @since: 2020/5/5 17:31
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
@WebServlet(urlPatterns = "/testServlet")
public class TestServlet extends HttpServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException,
IOException {
System.out.println("=============TestServlet=============");
}
}
三、验证:访问http://localhost:8084/citic/testServlet
四、添加测试filter的类:TestFilter
package com.example._06servlet;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* @Package: com.example._06servlet
* @ClassName: TestFilter
* @Description: Description:
* @author: zq
* @since: 2020/5/5 17:35
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
@WebFilter(urlPatterns = "/*")
public class TestFilter 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("======================TestFilter======================");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
五、测试验证:http://localhost:8084/citic/testServlet
六、添加测试listener的类:TestListener
package com.example._06servlet;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* @Package: com.example._06servlet
* @ClassName: TestListener
* @Description: Description:
* @author: zq
* @since: 2020/5/5 17:38
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
@WebListener
public class TestListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("==================启动了==================");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("==================关闭了==================");
}
}
七、验证测试:重新启动
2.通过组件注册器进行注册
一、启动类
package com.example._07servlet2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import java.util.Arrays;
/**
* @Package: com.example._06servlet
* @ClassName: AppConfig
* @Description: Description:
* @author: zq
* @since: 2020/5/5 17:33
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
@SpringBootApplication
//@ServletComponentScan
public class AppConfig {
//通过注册器注册servlet
@Bean
public ServletRegistrationBean testServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean();
bean.setServlet(new TestServlet());
bean.setUrlMappings(Arrays.asList("/testServlet"));
return bean;
}
public static void main(String[] args) {
SpringApplication.run(AppConfig.class, args);
}
}
二、将servlet的注解去掉
package com.example._07servlet2;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
/**
* @Package: com.example._06servlet
* @ClassName: TestServlet
* @Description: Description:
* @author: zq
* @since: 2020/5/5 17:31
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
//@WebServlet(urlPatterns = "/testServlet")
public class TestServlet extends HttpServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException,
IOException {
System.out.println("=============TestServlet=============");
}
}
三、测试验证:http://localhost:8084/citic/testServlet
四、启动类添加filter
package com.example._07servlet2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import java.util.Arrays;
/**
* @Package: com.example._06servlet
* @ClassName: AppConfig
* @Description: Description:
* @author: zq
* @since: 2020/5/5 17:33
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
@SpringBootApplication
//@ServletComponentScan
public class AppConfig {
//通过注册器注册filter
@Bean
public FilterRegistrationBean testFilter(){
FilterRegistrationBean bean=new FilterRegistrationBean();
bean.setFilter(new TestFilter());
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
//通过注册器注册servlet
@Bean
public ServletRegistrationBean testServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean();
bean.setServlet(new TestServlet());
bean.setUrlMappings(Arrays.asList("/testServlet"));
return bean;
}
public static void main(String[] args) {
SpringApplication.run(AppConfig.class, args);
}
}
package com.example._07servlet2;
import javax.servlet.*;
import java.io.IOException;
/**
* @Package: com.example._06servlet
* @ClassName: TestFilter
* @Description: Description:
* @author: zq
* @since: 2020/5/5 17:35
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
//@WebFilter(urlPatterns = "/*")
public class TestFilter 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("======================TestFilter======================");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
五、测试验证:http://localhost:8084/citic/testServlet
六、启动类添加listener
package com.example._07servlet2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import java.util.Arrays;
/**
* @Package: com.example._06servlet
* @ClassName: AppConfig
* @Description: Description:
* @author: zq
* @since: 2020/5/5 17:33
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
@SpringBootApplication
//@ServletComponentScan
public class AppConfig {
//通过注册器注册listener
@Bean
public ServletListenerRegistrationBean testListener(){
ServletListenerRegistrationBean bean=new ServletListenerRegistrationBean();
bean.setListener(new TestListener());
return bean;
}
//通过注册器注册filter
@Bean
public FilterRegistrationBean testFilter(){
FilterRegistrationBean bean=new FilterRegistrationBean();
bean.setFilter(new TestFilter());
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
//通过注册器注册servlet
@Bean
public ServletRegistrationBean testServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean();
bean.setServlet(new TestServlet());
bean.setUrlMappings(Arrays.asList("/testServlet"));
return bean;
}
public static void main(String[] args) {
SpringApplication.run(AppConfig.class, args);
}
}
package com.example._07servlet2;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* @Package: com.example._06servlet
* @ClassName: TestListener
* @Description: Description:
* @author: zq
* @since: 2020/5/5 17:38
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
//@WebListener
public class TestListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("==================启动了==================");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("==================关闭了==================");
}
}
七、验证测试,重新启动