先查看application.yml文件
spring:
data: notNull
profiles:
active: dev
application:
name: project
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
在一开始我使用的是@Value去获取对应的yml内容
如果需要输出spring->profiles->active在代码可以这样输出和使用
@Slf4j
public class ApplicationActive{
@Value("${spring.profiles.active}")
private String active;
public String findActive(){
return active;
}
}
在接触完一些git的代码后发现用@ConfigurationProperties注解去加载会更方便如果需要使用
@ConfigurationProperties(prefix = "xxx")
public class XxxConstants{
private String data;
private Application application;
}
如果需要使用Constans内的字段时,需要先加载Constants
public class XxxxxService{
@Resource
private XxxConstants xxxConstants;
public String getDatas(){
xxxConstants.getDatas();
}
}