Spring核心系列——多yaml数据读取,@Value,@ConfigurationProperties day1-1
多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;
}
}
测试结果如下: