springboot学习第二天
- Servlet创建和配置
- Filter创建和配置
- Listener创建和配置
- springboot资源配置源码分析
主要内容如下:
一、Servlet创建和配置
继承HttpServlet类,使用@WebServlet注解标记为Servlet;在启动类上加@ServletComponentScan注解扫描Servlet类
@WebServlet(name = "myServelt",urlPatterns = "/h2")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("1111");
super.doGet(req, resp);
}
}
@SpringBootApplication
@ServletComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
二、Filter创建和配置
自定义Filter需要实现Filter接口,使用@WebFilter注解(filterName、urlPatterns属性);urlPatterns为需要过滤的请求。
Filter类中主要有init()、doFilter()、destroy()方法
项目中如果存在多个Filter,可以使用@Order注解进行排序,value值越小优先级越高
@Order(value = 20)
@WebFilter(filterName = "webFilter",urlPatterns = "/*")
public class MyFilter1 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init()");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("doFilter()");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.println("destroy()");
}
}
三、Listener创建和配置
实现需要监听的监听器,如HttpSessionListener(用于统计系统在线人数等功能),相应注解@WebListener
@WebListener
public class MyHttpSessionLister implements HttpSessionListener {
public static int online;
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("sessionCreated");
online++;
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("sessionDestroyed");
online--;
}
}
四、springboot资源配置源码分析
主要类WebMvcAutoConfiguration、WebMvcAutoConfigurationAdapter
主要方法:addResourceHandlers
加载静态资源的三种方式如下:
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (this.servletContext != null) {
ServletContextResource resource = new ServletContextResource(this.servletContext, "/");
registration.addResourceLocations(new Resource[]{resource});
}
});
}
}
private void addResourceHandler(ResourceHandlerRegistry registry, String pattern, String... locations) {
this.addResourceHandler(registry, pattern, (registration) -> {
registration.addResourceLocations(locations);
});
}
private void addResourceHandler(ResourceHandlerRegistry registry, String pattern, Consumer<ResourceHandlerRegistration> customizer) {
if (!registry.hasMappingForPattern(pattern)) {
ResourceHandlerRegistration registration = registry.addResourceHandler(new String[]{pattern});
customizer.accept(registration);
registration.setCachePeriod(this.getSeconds(this.resourceProperties.getCache().getPeriod()));
registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl());
registration.setUseLastModified(this.resourceProperties.getCache().isUseLastModified());
this.customizeResourceHandlerRegistration(registration);
}
}
1、 this.addResourceHandler(registry, “/webjars/**”, “classpath:/META-INF/resources/webjars/”);
2、ResourceProperties、Resources类
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
静态资源存放目录4种,依次按优先级顺序