Spring核心系列——多yaml数据读取,@Value day1-1

181 篇文章 3 订阅
20 篇文章 2 订阅

多yaml数据读取

情景

一般来说我们的yaml用于对整个项目进行必要的如:jdbc,服务,redis 。。。等的配置,但现在我们希望我们能够在日后直接修改yaml配置文件做到修改程序返回值的时候,可能就会导致我们把大量数据写入一个application.yaml中,此时面对冗长的yaml文件在使用和阅读起来也会感觉很难受,于是我们就可以将一些业务分开,配置的放在application-config.yaml中,信息一类的放置在application.info.yaml中,还有一些其他的放置在application-mass.yaml

实现

1.创建三个yaml

在这里插入图片描述

application-mass.yaml
mass:
  msg: 我是杂物
application.info.yaml
user:
  username: zhangsan
  password: sjdkajdsd
application-config.yaml
proConfig:
  config-port: 8888

2.引入主yaml中

application.yaml中引入

spring:
  profiles:
    include:
      - info
      - config
      - mass

3.使用@Value注解进行读取

我们新建一个TestController,使用@Value注解进行注入

关键部分
    @Value("${user.username}")
    private String username;
    @Value("${user.password}")
    private String pwd;
    @Value("${mass.msg}")
    private String msg;
    @Value("${proConfig.config-port}")
    private String config;

以下是完整代码:

package com.example.test.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class TestController {
    @Value("${user.username}")
    private String username;
    @Value("${user.password}")
    private String pwd;
    @Value("${mass.msg}")
    private String msg;
    @Value("${proConfig.config-port}")
    private String config;

    @GetMapping("/test")
    public Object test() {
        HashMap<String, String> data = new HashMap<>();
        data.put(username, pwd);
        data.put("config",config);
        data.put("msg",msg);
        return data;
    }
}

4.启动测试

访问http://localhost:8080/test
结果如下:
在这里插入图片描述

@Value注解

我们都知道@Value常用于yaml配置注入,以下会介绍详细的使用

1.普通注入

使用@Value可以进行字符串的注入,当使用如下时:

@Value("user")
private String info;

直接写入数据表示将user字符串赋值给info变量
接下来我们写一个函数进行测试

@GetMapping("/testValue")
    public String testValue(){
        return info;
    }

测试结果如下:
在这里插入图片描述

2.yaml注入

@Value 时常用于进行yaml的注入
如我在yaml中写入数据如下:

application.yaml

msg: this is a msg for test

我们在controller中可以以如下形式进行注入:

controller

我们可以使用${}取值的方式进行获取对应yaml中的变量的值

	@Value("${msg}")
    private String msg;

测试结果如下:
在这里插入图片描述

3.Bean容器注入

我们可以使用@Value进行Bean容器的注入
首先我们先定义一个类如下:

package com.example.test.bean;

import org.springframework.stereotype.Component;

@Component("bean1")
public class TestBean {
}

在这个叫TestBean的类上加上@Component注解并起名为bean1
接下来我们在其他类中进行注入时使用#{}的方式,将TestBean进行注入,此时变量bean就被注入了TestBean,需要注意的是我们在写变量的类型时一定要和需要注入的类型一致

    @Value("#{bean1}")
    private TestBean bean;

在这里插入图片描述
测试结果如下:
在这里插入图片描述

基于@Value注解扩展

情景

当我们在开发时,在代码中使用@Value注解进行了大量的配置注入时,如:我在application.yaml中定义了一个username: zhangsan,在我的程序中又有大量的类使用到了这个username,此时你的产品经理要求你的username这个名字起的不好要改成nickname,这时我们就要去大量类中寻找我们用@Value("${username}")修改为@Value("${nickname}")十分繁琐!

面对这种情况我们可以采用自定义注解的方式对@Value注解进行扩展,实现修改单个注解即可修改全部的情况
以下是具体实现

实现

1.定义application.yaml

defineUser:
	username: zhangsan

2.自定义注解

在自定义的NameValueAnno注解中我们使用@Value注解进行注入,此时NameValueAnno注解也就被注入了zhangsan这个字符串,同时我们让其@Target和@Retention注解与@Value注解保持一致即可

package com.example.test.define.annocation;

import org.springframework.beans.factory.annotation.Value;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Value("${defineUser.username}")
public @interface NameValueAnno {
}

3.类中注入自定义注解

@RestController
public class TestController2 {
    @NameValueAnno
    private String name;

    @GetMapping("/name")
    public String getName(){
        return name;
    }
}

测试结果如下:

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值