简介:本文通过案例讲解SpringBoot如何yaml,yml,properties中的数据。
1.@Value
2. Environment
3. @ConfigurationProperties

name: abc
# 对象
person:
name: LIHUA
age: 20
# 对象行内写法
person1: {name: zhangsan, age: 20}
# 数组
address:
- beijing
- shanghai
# 数组的行内写法
address1: [wodetian, fangdefa]
# 纯量
msg1: 'hello \n lihua'
msg2: "hello \n lihua"学习代码
package com.example.springbootstudy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${name}")
private String name;
@Value("${person.name")
private String name2;
@Value("${person.age}")
private int age;
@Value("$address[0]")
private String address1;
@Value("${msg1}")
private String msg1;
@Value("${msg2}")
private String msg2;
@RequestMapping("/hello2")
public String hello2(){
System.out.println(name);
System.out.println(name2);
System.out.println(age);
System.out.println(address1);
System.out.println(address1);
System.out.println(msg1);
System.out.println(msg2);
return "hello Spring Boot";
}
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
}运行结果



学习代码
package com.example.springbootstudy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private Environment env;
@RequestMapping("/hello2")
public String hello2(){
System.out.println(env.getProperty("name"));
System.out.println(env.getProperty("person.name"));
System.out.println(env.getProperty("address[0]"));
return "hello Spring Boot";
}
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
}运行结果


文件结构

学习代码
package com.example.springbootstudy;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
@ConfigurationProperties(prefix = "person") // 这种方式需要定义前缀 然后才能在yml中找到正确的位置
// 这种方式是 自动化配对所以需要yml中的数据与Person类中的一致
public class Person {
private String name;
private int age;
private String [] address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getAddress() {
return address;
}
public void setAddress(String[] address) {
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + Arrays.toString(address) +
'}';
}
} package com.example.springbootstudy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private Person person;
@RequestMapping("/hello2")
public String hello2(){
System.out.println(person);
System.out.println(person.getName());
System.out.println(person.getAge());
return "hello Spring Boot";
}
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
}运行结果


一共有 n 个数,编号是 1∼n,最开始每个数各自在一个集合中。
现在要进行 m 个操作,操作共有两种:
M a b,将编号为 a 和 b 的两个数所在的集合合并,如果两个数已经在同一个集合中,则忽略这个操作; Q a b,询问编号为 a 和 b 的两个数是否在同一个集合中; 输入格式 第一行输入整数 n 和 m。
接下来 m 行,每行包含一个操作指令,指令为 M a b 或 Q a b 中的一种。
输出格式 对于每个询问指令 Q a b,都要输出一个结果,如果 a 和 b 在同一集合内,则输出 Yes,否则输出 No。
每个结果占一行。
数据范围 1≤n,m≤105 输入样例: 4 5 M 1 2 M 3 4 Q 1 2 Q 1 3 Q 3 4 输出样例: Yes No Yes
提交代码
#include<iostream>
using namespace std;
const int N = 100010;
int n, m;
int p[N];
int find(int x) // 找到x的祖先节点
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; ++i) p[i] = i;
while (m--)
{
char op;
int a, b;
scanf (" %c%d%d", &op, &a, &b);
if (op == 'M') p[p[find(a)]] = find(b); // 让a的祖先节点指向b的祖先节点
else
{
if (find(a) == find(b)) puts("Yes");
else puts("No");
}
}
return 0;
}import java.io.*;
public class Main
{
static int N = 100010;
static int n, m;
static int [] p = new int [N];
static int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader (System.in));
String [] str = reader.readLine().split(" ");
n = Integer.parseInt(str[0]);
m = Integer.parseInt(str[1]);
for (int i = 1; i <= n; ++ i) p[i] = i;
while (m -- > 0)
{
String op;
int a, b;
str = reader.readLine().split(" ");
op = str[0];
a = Integer.parseInt(str[1]);
b = Integer.parseInt(str[2]);
if (op.equals("M")) p[find(a)] = find(b);
else
{
if (find(a) == find(b)) System.out.println("Yes");
else System.out.println("No");
}
}
}
}