首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >SpringBoot获取配置中的数据

SpringBoot获取配置中的数据

作者头像
GeekLiHua
发布2025-01-21 16:07:46
发布2025-01-21 16:07:46
6730
举报
文章被收录于专栏:JavaJava

SpringBoot获取配置中的数据

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

方法

1.@Value 2. Environment 3. @ConfigurationProperties

环境准备

代码语言:javascript
复制
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
复制
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";
    }
}

运行结果

Environment

学习代码

代码语言:javascript
复制
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";
    }
}

运行结果

@ConfigurationProperties

文件结构

学习代码

Person
代码语言:javascript
复制
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) +
                '}';
    }
}
HelloController
代码语言:javascript
复制
    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

提交代码

代码语言:javascript
复制
#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;
}
代码语言:javascript
复制
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");
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-12-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot获取配置中的数据
    • 方法
    • 环境准备
    • @Value
    • Environment
    • @ConfigurationProperties
      • Person
      • HelloController
  • 合并集合
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档