在 SpringBootApplication 上使用@ServletComponentScan 注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码
Spring Boot 应用中这三个注解默认是不被扫描的,需要在项目启动类上添加 @ServletComponentScan 注解, 表示对 Servlet 组件扫描。
详细看下面几篇文章: Spring Boot @ServletComponentScan 扫描 @WebServlet、@WebFilter、@WebListener
Springboot注解@ServletComponentScan和@ComponentScan
@ServletComponentScan("com.Servlet")
@SpringBootApplication
public class CrudApplication {
public static void main(String[] args) {
SpringApplication.run(CrudApplication.class, args);
}
}
@WebServlet("/dhy")
public class Myservlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("servlet请求");
}
}
@Slf4j
@WebFilter("/main.html")
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.info("放行");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
log.info("销毁");
}
}
@WebListener
public class MyFilter implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("项目初始化中");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("项目销毁中");
}
}
ServletRegistrationBean, FilterRegistrationBean, and ServletListenerRegistrationBean
@Configuration
public class MyServletConfig
{
//注册三大组件
//注册Servlet到容器中
@Bean
public ServletRegistrationBean myServlet()
{
ServletRegistrationBean servletRegistrationBean =
//第一个参数:注册哪一个Servlet
//第二个参数:这个Servlet映射哪些路径
new ServletRegistrationBean(new MyServlet(),"/myServlet");
return servletRegistrationBean;
}
//注册Filter到容器中
@Bean
public FilterRegistrationBean myFilter()
{
FilterRegistrationBean registrationBean=new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
//拦截路径
registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return registrationBean;
}
//注册Listener到容器中
@Bean
public ServletListenerRegistrationBean myListener()
{
ServletListenerRegistrationBean<MyListener> listener = new ServletListenerRegistrationBean<>(new MyListener());
return listener;
}
//定制嵌入式的Servlet容器相关的规则
@Bean
public WebServerFactoryCustomizer webServerFactoryCustomizer(){
return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() {
//定制嵌入式的Servlet容器相关的规则
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setPort(8896);
}
};
}
}
Tomcat-Servlet; 多个Servlet都能处理到同一层路径,精确优选原则 A: /my/ B: /my/1
在pom.xml中排除tomcat依赖,再将要切换到的服务器的starter导入即可实现切换,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
一般修改默认属性建议在全局配置文件中设置或者实现WebServerFactoryCustomizer接口,重写相关方法,并放入容器中完成
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;
@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory server) {
server.setPort(9000);
}
}
无论是使用注解注册三大组件,还是使用ServletRegistrationBean方式注册三大组件,本质都是将原生的servlet被包装后放入到spring容器中,然后再注册到底层的servlet容器中
我们无法直接从spring容器中获取到三大组件的原因就在于其被包装过后,再被放入了ioc容器中
如果实在想获取,可以使用下面的方法:
@SpringBootApplication
public class WebFluxApplication {
static private ConfigurableApplicationContext app;
public static void main(String[] args)
{
app = SpringApplication.run(WebFluxApplication.class, args);
MyServletConfig bean = app.getBean(MyServletConfig.class);
System.out.println(bean);
ServletRegistrationBean myServlet = (ServletRegistrationBean)app.getBean("myServlet");
//这里返回的是servlet顶层接口,我们需要进行转换得到我们的原生servlet
servlet s = (servlet)myServlet.getServlet();
s.show();
}
}