SpringBoot注入Web原生组件

1.注解方式

@WebServlet

@WebFilter

@WebListener

@ServletComponentScan(basePackages = “com.wgd”) :
指定原生Servlet组件都放在哪里,写在主启动类上

2.配置类方式

servlet ==> ServletRegistrationBean
filter ==> FilterRegistrationBean
listener ==>ServletListenerRegistrationBean

/**
 * @ClassName MyRegisterConfig
 * @Description TODO
 * @Author Jelly
 * @DATE 2022/1/14 18:08
 * @Version IDEA2021.1
 */
@Configuration
public class MyRegisterConfig {
    //注册Servlet
    @Bean
    public ServletRegistrationBean myServlet() {
        MyServlet myServlet = new MyServlet();
        return new ServletRegistrationBean(myServlet, "/my");
    }
    //过滤器
    @Bean
    public FilterRegistrationBean myFilterRegistration() {
        return new FilterRegistrationBean();
    }
    //监听器
    public ServletListenerRegistrationBean myListenerRegistration() {
        return new ServletListenerRegistrationBean();
    }
}

你可能感兴趣的:(SpringBoot,spring,boot,java,开发语言,spring,intellij-idea)