1.@Value
1.本来想自己总结一下,查了一下发现这位同志写的真好,直接给链接吧 (点击跳转) ?
2.spring.profiles.active
1.有关于active 的,那篇文章有作解释,在此我再补充一下(点击跳转)?
还有一个注解 同样有获取配置的作用 @ConfigurationProperties
1.application.yml 文件
server:
port: 9098
spring:
application:
name: order-service
datasource:
url: jdbc:mysql://127.0.0.1:3306/shop?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 1000
test-on-borrow: true
redis:
host: 127.0.0.1
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
instance:
prefer-ip-address: true
ip-address: 127.0.0.1
instance-id: ${eureka.instance.ip-address}.${server.port}
lease-renewal-interval-in-seconds: 3
lease-expiration-duration-in-seconds: 10
sc:
jwt:
pubKeyPath: D:/rsa/rsa.pub # 公钥地址
2.JavaBean中使用
@Configuration //声明为配置类 让其可以扫描到
@Data //lombok
@ConfigurationProperties(prefix = "sc.jwt") //映射application.yml
public class JwtProperties {
private String pubKeyPath;// 公钥
private PublicKey publicKey; // 公钥
@PostConstruct
public void init(){
try {
// 获取公钥和私钥
this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
} catch (Exception e) {
System.out.println("初始化公钥失败!");
throw new RuntimeException();
}
}
}