在Spring Boot中,当需要以注解的方式添加session监听器或其他servlet(包括监听器、过滤器、以及自定义的servlet)时,为了让其生效,还需要在配置类上添加
@ServletComponentScan
注解,该注解的作用是告诉spring boot,让其自动管理servlet,否则注解方式注册的servlet不起作用
- 定义过滤器
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* 使用注解标注过滤器@WebFilter将一个实现了javax.servlet.Filter接口的类定义为过滤器
* filterName声明过滤器的名称(可选)
* urlPatterns指定要过滤的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性)
* <p>
* Created by surpass.wei@gmail.com on 2017/2/24.
*/
@WebFilter(filterName = "myFilter", urlPatterns = "/*")
public class MyFilter implements Filter {
private Logger logger = LoggerFactory.getLogger(MyFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException {
logger.info("过滤器:myFilter >>>> 初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
logger.info("过滤器:myFilter >>>> 执行过滤操作");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
logger.info("过滤器:myFilter >>>> 销毁");
}
}
- 定义监听器
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* HttpSession监听器
* <p>
* Created by surpass.wei@gmail.com on 2017/2/24.
*/
@WebListener
public class MyHttpSessionListener implements HttpSessionListener {
private Logger logger = LoggerFactory.getLogger(MyHttpSessionListener.class);
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
logger.info("监听器:Session >>>> 创建");
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
logger.info("监听器:Session >>>> 销毁");
}
}
- 自定义servlet
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 自定义Servlet
* <p>
* Created by surpass.wei@gmail.com on 2017/2/24.
*/
public class MyServlet1 extends HttpServlet {
private Logger logger = LoggerFactory.getLogger(MyServlet1.class);
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
logger.info("已执行:MyServlet1.doGet()");
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
logger.info("已执行:MyServlet1.doPost()");
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8"); // 设置编码,解决中文乱码问题
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>这是:MyServlet1</h1>");
out.println("</body>");
out.println("</html>");
}
}
配置@ServletComponentScan
以使以上servlet生效
@SpringBootApplication
@ServletComponentScan
public class BasicApp extends WebMvcConfigurerAdapter{
@Bean
public ServletRegistrationBean myServlet1() {
return new ServletRegistrationBean(new MyServlet1(),"/myServlet1/*");
}
public static void main(String[] args) {
SpringApplication.run(BasicApp.class, args);
}
}