Web项目的启动方式汇总

上一篇 << 下一篇 >>>SpringMVC底层无web.xml启动原理分析


Web项目的启动方式包括传统使用web.xml和不使用web.xml的方式。

1.使用传统的web.xml,并配置servlet和mapping


  WebXmlServlet
  com.jarye.webxml.WebXmlServlet

  
    WebXmlServlet
    /
  
public class WebXmlServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("success ");
    }
}

2.使用了传统的web.xml,不过servlet采用注解方式

@WebServlet("/")
public class AnnotationServlet extends HttpServlet {

    public int count = 0;

    public AnnotationServlet(){
        System.out.println("系统初始化完成");
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        count = count+1;
        resp.getWriter().write("success ,my god,count:"+count);
    }
}

3.使用无web.xml配置文件启动

/**
 *
 *  1、tomcat启动加载时自动检查/META-INF/services/javax.servlet.ServletContainerInitializer文件中的内容,
 *  必须实现ServletContainerInitializer接口,文件名称全部固定
 *  2、自定义类集成ServletContainerInitializer,完成初始化动作,可手动添加servlet、filter、listener等
 *  3、自定义类需要使用@HandlesTypes注解,且注解的子类自动加入到Set参数里,可随servletContext初始进web容器
 *  4、webapp下不能有web.xml文件
 *
 */
@HandlesTypes(MyHandel.class)
public class MyContainerInitializer implements ServletContainerInitializer {

    public void onStartup(Set> set, ServletContext servletContext) throws ServletException {

        for(Class c:set){
            System.out.println("c:"+c);
        }

        ServletRegistration.Dynamic defaultServlet = servletContext.addServlet("defaultServlet", new InitializerServlet());
        defaultServlet.addMapping("/");
//        servletContext.addFilter();
//        servletContext.addListener();

    }
}

4.SpringMVC集成启动无web.xml启动

引入jar包

    
      org.springframework
      spring-webmvc
      5.0.5.RELEASE
      compile
    

实现接口WebApplicationInitializer,即可自动加入到web容器中

public class MyWebApplicationInitializer implements WebApplicationInitializer {

    public void onStartup(javax.servlet.ServletContext servletContext)  {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(MyConfig.class);
        ServletRegistration.Dynamic defaultServlet = servletContext.addServlet("defaultServlet", new DispatcherServlet(context));
        defaultServlet.addMapping("/");
        defaultServlet.setLoadOnStartup(1);
        defaultServlet.setAsyncSupported(true);
    }
}
@Configuration
@ComponentScan(basePackages = {"com.jarye.controller", "com.jarye.service"})
//@EnableAsync
@EnableWebMvc
//public class MyConfig extends WebMvcConfigurationSupport {

public class MyConfig implements WebMvcConfigurer {
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver(){
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/WEB-INF/view/");
        internalResourceViewResolver.setSuffix(".jsp");
        return internalResourceViewResolver;
    }

    @Bean
    public MyInterceptor myInterceptor(){
        return new MyInterceptor();
    }

    /**
     *  拦截器注入方式:
     *  方法1:通过继承WebMvcConfigurationSupport类,但加上注解@EnableWebMvc会把当前的拦截器覆盖,导致无效
     *  方法2:通过实现WebMvcConfigurer接口,可以和注解@EnableWebMvc同时使用
     *
     */
//    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor()).addPathPatterns("/**");
    }
}

推荐阅读:
<< << << << << <<<过滤器与拦截器的区别
<< << <<<@Async注解的失效之谜
<<

你可能感兴趣的:(Web项目的启动方式汇总)