欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Web.xml零配置

程序员文章站 2022-04-30 15:33:46
...


Servlet3.0+规范允许servlet、filter、listenner不必在声明在web.xml文件中,可以直接通过编码的方式实现零配置化。

去除web.xml文件

web容器启动时会自动扫描当前服务中的ServletContainerInitializer的实现类,通过调用onStartup方法实现装配,其参数Set<Class<?>> c,可通过在实现类上声明注解javax.servlet.annotation.HandlesTypes(xxx.class)注解自动注入,@HandlesTypes会自动扫描项目中所有的xxx.class的实现类,并将其全部注入Set。

import java.util.Set;
public interface ServletContainerInitializer {
    void onStartup(Set<Class<?>> var1, ServletContext var2) throws ServletException;
}

Spring中已经提供ServletContainerInitializer的实现类SpringServletContainerInitializer。

@HandlesTypes({WebApplicationInitializer.class})
public class SpringServletContainerInitializer implements ServletContainerInitializer {
    public SpringServletContainerInitializer() {
    }

    public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException {
        List<WebApplicationInitializer> initializers = new LinkedList();
        Iterator var4;
        if (webAppInitializerClasses != null) {
            var4 = webAppInitializerClasses.iterator();

            while(var4.hasNext()) {
                Class<?> waiClass = (Class)var4.next();
                if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) && WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
                    try {
                        initializers.add((WebApplicationInitializer)waiClass.newInstance());
                    } catch (Throwable var7) {
                        throw new ServletException("Failed to instantiate WebApplicationInitializer class", var7);
                    }
                }
            }
        }

        if (initializers.isEmpty()) {
            servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
        } else {
            AnnotationAwareOrderComparator.sort(initializers);
            servletContext.log("Spring WebApplicationInitializers detected on classpath: " + initializers);
            var4 = initializers.iterator();

            while(var4.hasNext()) {
                WebApplicationInitializer initializer = (WebApplicationInitializer)var4.next();
                initializer.onStartup(servletContext);
            }

        }
    }
}

我们可以看出WebApplicationInitializer.class 才是我们关注的接口,只要在该类上注入我们需要的filter、listenner既可以取代web.xml配置文件的作用。

实现WebApplicationIinitializer

public class MyWebApplicationInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext webContext = createWebContext(RootConfig.class);
        ServletRegistration.Dynamic dynamic = servletContext.addServlet("zjl_platform_api", new AppDispatcherServlet(webContext));
        dynamic.addMapping("/");
        dynamic.setLoadOnStartup(1);

    }
    /**
     * 自定义配置类来实例化一个Web Application Context
     * @param annotatedClasses
     * @return
     */
    private AnnotationConfigWebApplicationContext createWebContext(Class<?>... annotatedClasses) {
        AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
        webContext.register(annotatedClasses);
        return webContext;
    }
}

注:RootConfig.class 为springMvc相关的注解配置