zoukankan      html  css  js  c++  java
  • 内嵌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();
    }



    }
  • 相关阅读:
    MongoDB学习笔记(二)
    mongoDB学习笔记(一)
    docker官方文档笔记
    nagios
    网络流量状态命令总结 (含notp安装)
    other
    一键搭建LNMP脚本
    linux问题总结
    linux中VI编写C程序。。。
    centos 7 安装python3.5.1
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/13362178.html
Copyright © 2011-2022 走看看