zoukankan      html  css  js  c++  java
  • 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 + " ~~";
        }
    }
  • 相关阅读:
    文件路径选择中的三态逻辑
    .net版本号
    使用MSBuild编译vs多个解决方案
    CEF截图
    使用SharpZIpLib写的压缩解压操作类
    软件试用期设置
    list转datatable
    excel 导入
    网站登录简单验证码
    UEditor编辑器
  • 原文地址:https://www.cnblogs.com/tugeler/p/6541735.html
Copyright © 2011-2022 走看看