新建maven项目,导入依赖,不需要配置
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.1.8.RELEASEversion>
- parent>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- package com.pro;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- /**
- * springboot 的启动类
- */
- @SpringBootApplication
- public class BootApp {
- public static void main(String[] args) {
- SpringApplication.run(BootApp.class,args);
- }
- }
- package com.pro.controller;
-
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- public class HelloController {
-
- @GetMapping("/hai")
- public String hai(){
- return "hello world";
- }
- }
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
- #系统提供的,我们引出来,填值就可以了
- server:
- port: 9999
- servlet:
- context-path: /boot2
-
-
- #自定义的配置数据,也可以在这里配置,在controller里面如何获取?
- #字符串
- dd: 俊俊
- #对象
- person:
- name: zs
- #数组
- address:
- - hz
- - cq
- #单,双引号,有不同的含义,双引号识别换行,会换行,单引号忽略换行
- msg: 'hello \n world'
- msg1: "hello \n world1"
- #引用已经定义的名称
- mmssgg: hello${dd}
- mmssgg1: 'hello ${dd}'
controler中取:
- package com.pro.controller;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class HelloController {
-
- @Value("${dd}")
- private String dd;
- @Value("${person.name}")
- private String perName;
- @Value("${address[0]}")
- private String address;
- @Value("${msg}")
- private String msg;
- @Value("${msg1}")
- private String msg1;
- @Value("${mmssgg}")
- private String mmssgg;
- @Value("${mmssgg1}")
- private String mmssgg1;
- @GetMapping("/hai")
- public String hai(){
- return "hello world"+perName+"=="+address+"=="+msg+"=="+msg1+"=="+mmssgg+"=="+mmssgg1;
- }
- }
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-configuration-processorartifactId>
- <version>2.1.7.RELEASEversion>
- <optional>trueoptional>
- dependency>
在导入依赖,下载的时候可能会报这个错
前两种主要是针对单个数据去赋值,如果是对象有多个属性,就没法赋值了
第三种针对对象的多个属性可以赋值