spring boot项目读取配置文件的参数

137 阅读1分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。​​​​​

我们在接入用户系统时需要将接口地址,错误地址,调用地址写入到配置文件里,我们使用的是.yml文件,在application.yml中写入需要写入的地址.yml必须规定格式编写

#根据token获取用户信息的接口地址 token-url: "xxx"

#错误返回的地址 error-url: "xxx"

#正确返回的地址 success-url: "xxx"

在代码中直接通过@Value("参数名")方式取值

示例

public class Demo {

    @Value("${token-url}")
    private String url;

    @Value("${error-url}")
    private String errorUrl;

    @Value("${success-url}")
    private String successUrl;

    public static void main(String[] args) {
        int n = 8;
        String[][] arr = demo(n);


        String print = "";
        for (int j = 0; j < n; j++) {
            for (int i = 0; i < n; i++) {
                print = print + arr[i][j] + " ";
            }
            print = print + "\n";
        }
        new HashSet();
        new Hashtable();
        new HashMap();
        new ReentrantLock(true).lock();
        new ReentrantLock().unlock();

        System.out.print(print);
    }


    public synchronized static String[][] demo(int n){
        int index = 0; // 一维数组下标
        int line = 0; // 二维数组下标
        //0右,1下,2左,3上
        int direction = 0;
        int x2 = n; // 定义行数
        int y2 = n; // 定义列数

        String[][] arr = new String[n][n]; // 初始化二维数组
        for (int i = 0, size = n * n; i < size; i++) { // 循环整个矩阵,i不动,调整写入的二维数组位置
            if (i < 10) { // 如果小于10为两位数+0
                arr[index][line] = "0" + i + "";
            } else {
                arr[index][line] = i + "";
            }
            // 操作二维数组位置
            if (direction == 0) {
                if (index == x2 - 1) { // 判断是否达到行/列最大下标,true则该准备转行,direction为上下左右的代表
                    direction++;
                } else {
                    index++; // 为上侧正序方式的位置,行数不动,列数++
                }
            }
            if (direction == 1) {
                if (line == y2 - 1) { // 判断是否达到行/列最大下标,true则该准备转行,direction为上下左右的代表
                    direction++;
                } else {
                    line++; // 为右侧正序方式的位置,列数不动,行数++
                }
            }
            if (direction == 2) {
                if (index == n - x2) { // 判断是否达到行/列最大下标,true则该准备转行,direction为上下左右的代表
                    direction++;
                    y2--; // 因为从右侧上往下,当前列数满,所以列数-1,倒数第二位开始处理
                } else {
                    index--; // 为下侧倒叙方式的位置,行数不动,列数--
                }
            }
            if (direction == 3) {
                if (line == n - y2) { // 判断是否达到行/列最大下标,true则该准备转行,direction为上下左右的代表
                    direction = 0;
                    x2--; // 因为最后一行数值已满,倒数第二位开始处理
                    index++; // 因为第一行数值已满,到第二位结束处理
                } else {
                    line--; // 为左侧倒叙方式的位置,列数不动,行数--
                }
            }
            System.out.print(">>>>  行数:"+index+" 列数:"+line+" 位置:"+direction+ "数值:"+ i +"\n");

        }
        new Thread();
        return  arr;
    }

}