SpringBoot启动模拟

结合Tomcat不使用web.xml加载Springmvcpom <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context<...
摘要由CSDN通过智能技术生成

结合Tomcat不使用web.xml加载Springmvc

pom

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>8.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-el</artifactId>
            <version>8.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>8.5.5</version>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
        </plugins>
    </build>

启动springmvc的配置

@Configuration
@ComponentScan("com.demo")
public class AppConfig {

}
/**
 * @author zc
 * @version 1.0
 * @date 2020/1/12 1:04 下午
 * @desc
 */
public class MyWebAppInit implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        System.out.println("+++++++++++++++++++++");

        // Load Spring web application configuration
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
        ac.register(AppConfig.class);
        ac.refresh();

        // Create and register the DispatcherServlet
        DispatcherServlet servlet = new DispatcherServlet(ac);
        ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
        registration.addMapping("/");
        registration.setLoadOnStartup(1);
    }
}

启动tomcat

/**
 * @author zc
 * @version 1.0
 * @date 2020/1/12 2:42 下午
 * @desc
 */
public class MySpringApplication {
    public static void run() throws ServletException, LifecycleException {

        Tomcat tomcat = new Tomcat();
        tomcat.setPort(9090);


        //告訴tomcat你的源码在哪里
        String sourcePath = MySpringApplication.class.getResource("/").getPath();
        Context context = tomcat.addWebapp("/", new File("src/main/webapp").getAbsolutePath());
        WebResourceRoot resources = new StandardRoot(context);
        resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes",
                sourcePath, "/"));
        context.setResources(resources);


        tomcat.start();
        tomcat.getServer().await();
    }
}

controller

@Controller
public class UserController {

    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        System.out.println("***************");
        return "hello spring mvc";
    }
}

启动类

public class MySpringStart {
    public static void main(String[] args) throws ServletException, LifecycleException {
        MySpringApplication.run();
    }
}

项目顺利启动

访问一下
image.png

真正的启动方式

上面的方式并不是springboot真正的启动方式,他是不依赖ServletContainerInitializer的

注意下面这行代码,addWebApp之后,tomcat就会认为你要启动的是一个web项目,此时tomcat才回去执行实现ServletContainerInitializer的类

Context context = tomcat.addWebapp("/", new File("src/main/webapp").getAbsolutePath());

如果没有这行代码,就无法解析
我们来修改一下run方法

public class MySpringApplication {
    public static void run() throws ServletException, LifecycleException {

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


        // Load Spring web application configuration
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
        ac.register(AppConfig.class);
        ac.refresh();


        Tomcat tomcat = new Tomcat();
        tomcat.setPort(9090);


        //告訴tomcat你的源码在哪里
        String sourcePath = MySpringApplication.class.getResource("/").getPath();
//        Context context = tomcat.addWebapp("/", file.getAbsolutePath());

        Context context = tomcat.addContext("/", file.getAbsolutePath());



        DispatcherServlet dispatcherServlet = new DispatcherServlet(ac);
        Tomcat.addServlet(context, "testServlet", dispatcherServlet);
        context.addServletMapping("/", "testServlet");


        tomcat.start();
        tomcat.getServer().await();
    }
}

注意下面的代码

Context context = tomcat.addContext("/", file.getAbsolutePath());

这种情况下tomcat就不会去解析ServletContainerInitializer的实现类
所以我们就要在这个方法中去初始化spring环境,然后把servlet交给tomcat.

所以最后只需要这个run方法和AppConfig.java即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值