SpringBoot中的profile的使用

简介: SpringBoot中的profile的使用

SpringBoot中的profile的使用

简介:本文通过案例讲解SpringBoot中的profile的使用。

概述

profile是用来完成不同环境下的,配置的动态切换功能的。

配置方式

properties

这里是三种不同的properties文件,我们想要运行哪一个,可以在第一个application.properties文件中指定.

application-dev.properties

application-pro.properties

application-test.properties

spring.profiles.active=test # 指定pro为前缀的properties为主要运行文件

yml

文件结构

学习代码

---
server:
  port: 8081
spring:
  profiles: div
---
server:
  port: 8082
spring:
  profiles: test
---
server:
  port: 8083
spring:
  profiles: pro
---
spring:
  profiles:
    active: pro

运行结果

程序打包运行

合并集合

一共有 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");
            }
        }
    }
}
相关文章
|
5月前
|
Java 测试技术 数据库
SpringBoot:@Profile注解和Spring EL
SpringBoot:@Profile注解和Spring EL
|
5月前
|
Java 测试技术 数据库
详解SpringBoot中的profile
详解SpringBoot中的profile
59 0
|
Java 应用服务中间件 Maven
解析Spring Boot中的Profile:配置文件与代码的双重掌控
解析Spring Boot中的Profile:配置文件与代码的双重掌控
|
算法 Java 测试技术
SpringBoot@Profile详解
SpringBoot@Profile详解
184 0
|
3月前
|
Java Spring
深入理解Spring Boot中的Profile配置
深入理解Spring Boot中的Profile配置
|
5月前
|
Java Spring
SpringBoot中多Profile使用与切换
SpringBoot中多Profile使用与切换
81 0
|
11月前
|
Java Spring
Spring Boot 启动报错解决:No active profile set, falling back to default profiles: default
Spring Boot 启动报错解决:No active profile set, falling back to default profiles: default
298 0
|
12月前
|
Java
07 SpringBoot之Profile文件
07 SpringBoot之Profile文件
41 0
|
Java Nacos Spring
使用Spring Boot的Profile功能来实现不同环境使用不同的Nacos Namespace的配置
使用Spring Boot的Profile功能来实现不同环境使用不同的Nacos Namespace的配置
528 1
|
Java Spring
Spring Boot入门(八) 之Profile多环境支持
Spring Boot入门(八) 之Profile多环境支持
下一篇
无影云桌面