- spring方式注入servlet组件:普通类 + ServletRegistrationBean + @ServletComponentScan
# 编写普通类
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("66666");
}
}
# 新建配置类并标注为一个组件
@Configuration(proxyBeanMethods = true)
public class MyRegistConfig {
@Bean
public ServletRegistrationBean myServlet(){
MyServlet myServlet = new MyServlet(); # new一个普通类的对象,获取的是他的业务逻辑
return new ServletRegistrationBean(myServlet,"/my","/my02"); # 参数为对象和请求路径
}
}
# 扫描
@MapperScan("com.atguigu.admin.mapper")
@ServletComponentScan(basePackages = "com.atguigu.admin")
@SpringBootApplication(exclude = RedisAutoConfiguration.class)
public class Boot05WebAdminApplication {
public static void main(String[] args) {
SpringApplication.run(Boot05WebAdminApplication.class, args);
}
}
- spring方式注入filter组件:普通类 + FilterRegistrationBean + @ServletComponentScan
# 方式1
# 编写普通类
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("66666");
}
}
# 新建配置类并标注为一个组件
@Configuration(proxyBeanMethods = true)
public class MyRegistConfig {
@Bean
public ServletRegistrationBean myServlet(){
MyServlet myServlet = new MyServlet(); # new一个普通类的对象,获取的是他的业务逻辑
return new ServletRegistrationBean(myServlet,"/my","/my02"); # 参数为对象和请求路径
}
@Bean
public FilterRegistrationBean myFilter(){
MyFilter myFilter = new MyFilter();
return new FilterRegistrationBean(myFilter,myServlet()); // 这里直接传入上面myServlet的路径,表示拦截该路径
return filterRegistrationBean;
}
}
# 扫描
@MapperScan("com.atguigu.admin.mapper")
@ServletComponentScan(basePackages = "com.atguigu.admin")
@SpringBootApplication(exclude = RedisAutoConfiguration.class)
public class Boot05WebAdminApplication {
public static void main(String[] args) {
SpringApplication.run(Boot05WebAdminApplication.class, args);
}
}
# 方式2
# 新建配置类并标注为一个组件
@Configuration(proxyBeanMethods = true)
public class MyRegistConfig {
@Bean
public FilterRegistrationBean myFilter(){
MyFilter myFilter = new MyFilter();
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*"));
return filterRegistrationBean;
}
}
# 扫描
@MapperScan("com.atguigu.admin.mapper")
@ServletComponentScan(basePackages = "com.atguigu.admin")
@SpringBootApplication(exclude = RedisAutoConfiguration.class)
public class Boot05WebAdminApplication {
public static void main(String[] args) {
SpringApplication.run(Boot05WebAdminApplication.class, args);
}
}
- spring方式注入listener组件:ServletListenerRegistrationBean + @ServletComponentScan
# 新建配置类
@Configuration(proxyBeanMethods = true)
public class MyRegistConfig {
@Bean
public ServletListenerRegistrationBean myListener(){
MySwervletContextListener mySwervletContextListener = new MySwervletContextListener();
return new ServletListenerRegistrationBean(mySwervletContextListener);
}
}
# 扫描
@MapperScan("com.atguigu.admin.mapper")
@ServletComponentScan(basePackages = "com.atguigu.admin")
@SpringBootApplication(exclude = RedisAutoConfiguration.class)
public class Boot05WebAdminApplication {
public static void main(String[] args) {
SpringApplication.run(Boot05WebAdminApplication.class, args);
}
}
@Configuration(proxyBeanMethods = true)