1、准备实现类
filter
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("filter的初始化方法。。。");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("filter执行功能方法。。。");
chain.doFilter(request, response);
}
@Override
public void destroy() {
System.out.println("filter销毁方法。。。");
}
}
servlet
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("servlet方法");
}
}
listener
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("监听器的初始化方法。。。");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("监听器的销毁方法。。。");
}
}
注入方式一
使用配置类的方式
import java.util.Arrays;
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 org.springframework.context.annotation.Configuration;
/**
* 配置类
*/
@Configuration
public class WebConfig {
/**
* 注入servlet
* @return
*/
@Bean
public ServletRegistrationBean<MyServlet> myServlet(){
ServletRegistrationBean<MyServlet> registrationBean = new ServletRegistrationBean<>(new MyServlet(),"/myServlet");
registrationBean.setLoadOnStartup(1);
return registrationBean;
}
/**
* 注入filter
* @return
*/
@Bean
public FilterRegistrationBean<MyFilter> myFilter(){
FilterRegistrationBean<MyFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new MyFilter());
registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return registrationBean;
}
/**
* 注入listener
* @return
*/
@Bean
public ServletListenerRegistrationBean<MyListener> myListener(){
ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
return registrationBean;
}
}
注入方式二
以注解的方式,分别在三个组件的类上加入注解
@WebFilter(urlPatterns = {"/hello","/myFilter"})
public class MyFilter implements Filter {
...
}
@WebServlet(urlPatterns = {"/myServlet"})
public class MyServlet extends HttpServlet {
...
}
@WebListener
public class MyListener implements ServletContextListener {
...
}
在springboot的启动类加上扫描注解
@SpringBootApplication
@ServletComponentScan
public class TestSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(TestSpringBootApplication.class, args);
}
}
使用注解的好处是,在组件中可以直接注入spring容器中的bean
@WebFilter(urlPatterns = {"/hello","/myFilter"})
public class MyFilter implements Filter {
@Autowired
private IUserService userservice;
...
}
2、获取spring容器中bean的方式
当使用配置类时,在组件中获取spring容器中的bean的方式
①、filter中
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("filter的初始化方法。。。");
ServletContext servletContext = filterConfig.getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//获取bean
IUserService userservice = (IUserService)ctx.getBean("userservice");
}
②、servlet中
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("servlet方法");
ServletContext servletContext = req.getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//获取bean
IUserService userservice = (IUserService)ctx.getBean("userservice");
}
③、listener中
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("监听器的初始化方法。。。");
ServletContext servletContext = sce.getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//获取bean
IUserService userservice = (IUserService)ctx.getBean("userservice");
}