需要使用的注解:
1. @Component
2. @PropertySource("classpath:application.yml") //设置需要读取配置文件的路径
3. @ConfigurationProperties(prefix = "spring.redis") //设置配置至哪个节点,下面举例说明
4. @Value //声明绑定的值
一. 创建application.yml文件
spring:
redis:
host: 172.31.108.46
port: 8080
pool:
max-active: -1
max-idle: 5
max-wait: -1
min-idle: 10
二. 创建与之对应的实体类
我们直接将实体类对应至spring.redis下, 所以字段直接设置为spring.redis下的字段即可。
如果下面仍有嵌套操作, 可以使用@Value注解进行生命,用${}进行拼接。
如果需要的话,可以分别设置实体类与yml文件的字段名称,并用@Value注解进行绑定声明即可。
package cn.com.redistest.Utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.a