• Spring-boot 最小demo


    POM 文件,注意红色部分:

    <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>spring-boot-starter-data-mongodb</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
        <build>
         <!-- SpringBoot打包插件 ( 打包好的Jar可以直接用“java -jar YOUR_JAR.jar”命令启动 ) --> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>

    程序入口:

    package com.cui;
    
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application implements CommandLineRunner {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @Override
        public void run(String... args) throws Exception {
            System.out.println("Spring加载完后执行的逻辑,可以直接使用Spring上下文,以及依赖注入@Autowired");
        }
    }

    最简单的Web应用 (然而这并不是重点,仅仅是POM中spring-boot-starter-web组建的简单示例, 更多组件参考 http://www.mvnrepository.com/search?q=spring-boot-starter- ):

    package com.cui;
    
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/user")
    public class UserController {
        @RequestMapping("/{id}")
        public String view(@PathVariable("id") Long id) {
            return id + " ~~";
        }
    }
  • 相关阅读:
    JAVA与编译语言及解释语言的关系
    Java虚拟机工作原理详解
    VMWare 网络连接模式(bridged、NAT、host-only)详解
    Ubuntu修改语言环境为英文
    Ubuntu安装和设置SSH服务
    Eclipse NDK 配置
    2018年长沙理工大学第十三届程序设计竞赛
    埃森哲杯第十六届上海大学程序设计联赛春季赛暨上海高校金马五校赛
    算法训练 安慰奶牛
    算法训练 最短路
  • 原文地址:https://www.cnblogs.com/tugeler/p/6541735.html
Copyright © 2020-2023  润新知