美文网首页
springmvc内嵌tomcat

springmvc内嵌tomcat

作者: 不知不怪 | 来源:发表于2022-01-07 15:55 被阅读0次

    1.pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.gzz</groupId>
        <artifactId>03-spring-mvc-tomcat</artifactId>
        <version>1.0.0</version>
        <packaging>jar</packaging>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.3.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
                <version>9.0.56</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
    

    2.配置类

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

    3.启动类

    package com.gzz;
    
    import java.io.File;
    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 com.gzz.config.AutoConfig;
    
    public class Application {
    
        public static void main(String[] args) throws Exception {
            AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
            context.register(AutoConfig.class);
            context.refresh();
    
            Tomcat tomcat = new Tomcat();
            Context context1 = tomcat.addContext("/", new File(System.getProperty("java.io.tmpdir")).getAbsolutePath());
            Tomcat.addServlet(context1, "my", new DispatcherServlet(context)).setLoadOnStartup(1);
            context1.addServletMappingDecoded("/", "my");
            
            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();
        }
    
    }
    
    

    4.Controller

    package com.gzz.sys.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class UserController {
        @GetMapping("hello")
        public String index() {
            return "hello";
        }
    }
    
    

    相关文章

      网友评论

          本文标题:springmvc内嵌tomcat

          本文链接:https://www.haomeiwen.com/subject/apbjcrtx.html