• 内嵌Tomcat和SpringMVC的简单整合测试


    1、首先是包的依赖

     2、添加对外暴露的接口类

    package com.fh.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class IndexController {
    
    
        public IndexController(){
            System.out.println("indexController初始化");
        }
    
    
        @GetMapping("index")
        @ResponseBody
        public String index(){
            return "index";
        }
    }
    View Code

    3、添加配置类信息

    package com.fh.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan("com.fh")
    public class AutoConfig {
    }
    View Code

    4、添加主类进行SpringWeb环境的初始化和Tomcat的启动

    package com.fh;

    import com.fh.config.AutoConfig;
    import org.apache.catalina.Context;
    import org.apache.catalina.Server;
    import org.apache.catalina.connector.Connector;
    import org.apache.catalina.startup.Tomcat;
    import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
    import org.springframework.web.servlet.DispatcherServlet;

    import java.io.File;

    public class ExampleMvcApplication {

    public static void main(String[] args) throws Exception {
    AnnotationConfigWebApplicationContext context =
    new AnnotationConfigWebApplicationContext();
    context.register(AutoConfig.class);
    context.refresh();

    File base = new File(System.getProperty("java.io.tmpdir"));

    Tomcat tomcat = new Tomcat();
    /**
    * web项目
    * contextPath tomcat的访问路径
    * 项目的web目录
    *
    * 会调用相关的初始化实现类
    */
    // Context context1 = tomcat.addWebapp("/", base.getAbsolutePath());

    DispatcherServlet dispatcherServlet = new DispatcherServlet(context);

    Context context1 = tomcat.addContext("/", base.getAbsolutePath());
    //tomcat启动的过程中会调用DispatcherServlet中的init方法
    Tomcat.addServlet(context1,"my",dispatcherServlet).setLoadOnStartup(1);
    //spring web环境
    context1.addServletMappingDecoded("/","my");
    // context1.addServletMapping("/","my");

    //tomcat8
    // tomcat.setPort(8080);
    // tomcat.start();
    // tomcat.getServer().await();


    //tomcat9
    Server server = tomcat.getServer();
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setPort(8080);
    connector.setThrowOnFailure(true);
    tomcat.getService().addConnector(connector);
    tomcat.setConnector(connector);
    tomcat.start();
    server.await();
    }



    }
  • 相关阅读:
    hdu4622(后缀自动机模板)
    51nod1445(最短路)
    Java原子类--框架
    Java锁--Semaphore
    Java锁--CyclicBarrier
    Java锁--CountDownLatch
    Java锁--共享锁和ReentrantReadWriteLock
    Java锁--LockSupport
    Java锁--Condition
    Java锁--非公平锁
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/13362178.html
Copyright © 2020-2023  润新知