1. 程式人生 > >springboot的簡單搭建(一)

springboot的簡單搭建(一)

專案我去搭建一個。

先貼出整個SpringBoot專案的結構 
這裡寫圖片描述

1.首先新建一個web專案 
2.修改pom.xml配置檔案,引入SpringBoot的核心依賴

<dependencies>
        <!--SpringBoot核心模組,包括自動配置支援、日誌和YAML-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--測試模組,包括JUnit、Hamcrest、Mockito-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--Web模組-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3.新建一個啟動類,SpringBoot專案都有一個啟動類,執行SpringBoot專案時,直接執行啟動類即可(啟動類好像只能放在java目錄下,最外層包中,如我的啟動類放在com.example.demo包中)

/**
 * SpringBoot專案啟動類
 */
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        //啟動時關閉Banner
//        SpringApplicationBuilder builder = new SpringApplicationBuilder(DemoApplication.class);
//        builder.bannerMode(Banner.Mode.OFF).run(args);

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4.在resources目錄下新建一個application.yml或application.properties配置檔案(.yml和.properties都可以,看個人喜好,瞭解yml語法之後個人偏向於使用yml)

#服務配置資訊
server:
  address: localhost
  context-path: /lilu
  port: 8080
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

基本搭建已經完成,接下來可以寫自己的請求介面

@RestController
public class HelloController {
    @Value("${person.name}")
    private String name;

    @RequestMapping("/hello")
    public String hello(){
        System.out.println(name);
        return "hello"+name;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

這裡@Value中的${person.name}是我在yml配置檔案中配置過的

#服務配置資訊
server:
  address: localhost
  context-path: /lilu
  port: 8080
  tomcat:
    uri-encoding: utf-8
person:
 name: 藜蘆
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

這裡寫圖片描述