您现在的位置是:首页 > 编程 > 

SpringBoot获取配置中的数据

2025-07-27 19:34:22
SpringBoot获取配置中的数据 SpringBoot获取配置中的数据简介:本文通过案例讲解SpringBoot如何yaml,yml,properties中的数据。方法1.@Value 2. Environment . @ConfigurationProperties环境准备代码语言:javascript代码运行次数:0运行复制name: abc # 对象 person: name

SpringBoot获取配置中的数据

SpringBoot获取配置中的数据

简介:本文通过案例讲解SpringBoot如何yaml,yml,properties中的数据。

方法

1.@Value 2. Environment . @ConfigurationProperties

环境准备

代码语言:javascript代码运行次数:0运行复制
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"

@Value

学习代码

代码语言:javascript代码运行次数:0运行复制
package 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("${")
    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(){
        println(name);
        println(name2);
        println(age);
        println(address1);
        println(address1);
        println(msg1);
        println(msg2);
        return "hello Spring Boot";
    }
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World";
    }
}

运行结果

Environment

学习代码

代码语言:javascript代码运行次数:0运行复制
package springbootstudy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.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(){
        println(env.getProperty("name"));
        println(env.getProperty(""));
        println(env.getProperty("address[0]"));
        return "hello Spring Boot";
    }
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World";
    }
}

运行结果

@ConfigurationProperties

文件结构

学习代码

Person
代码语言:javascript代码运行次数:0运行复制
package springbootstudy;

import org.springframework.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 getame() {
        return name;
    }

    public void setame(String 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=" + (address) +
                '}';
    }
}
HelloController
代码语言:javascript代码运行次数:0运行复制
    package springbootstudy;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.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(){

            println(person);
            println(person.getame());
            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,否则输出 o。

每个结果占一行。

数据范围 1≤n,m≤105 输入样例: 4 5 M 1 2 M 4 Q 1 2 Q 1 Q 4 输出样例: Yes o Yes

提交代码

代码语言:javascript代码运行次数:0运行复制
#include<iostream>
using namespace std;

ct int  = 100010;
int n, m;
int p[];

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("o");
        }
    }
    return 0;
}
代码语言:javascript代码运行次数:0运行复制
import java.io.*;

public class Main
{
    static int  = 100010;
    static int n, m;
    static int [] p = new int [];
    
    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 (("M")) p[find(a)] = find(b);
            else 
            {
                if (find(a) == find(b)) println("Yes");
                else println("o");
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2022-12-29,如有侵权请联系 cloudcommunity@tencent 删除配置数据intstring集合

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1150341.html

相关标签:无
上传时间: 2025-07-19 22:03:45
留言与评论(共有 5 条评论)
本站网友 古交二手房出售
5分钟前 发表
m; static int [] p = new int []; 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
本站网友 今日国际新闻
25分钟前 发表
每行包含一个操作指令
本站网友 海岩的儿子
26分钟前 发表
20 # 对象行内写法 person1
本站网友 嘉旺茶餐厅
8分钟前 发表
- beijing - shanghai # 数组的行内写法 address1