1. @value("${name}")
配置文件里聲明(application.yml)
frank:
springboot:
name: config
使用
@Value("${frank.springboot.version}")
private String version;
2.使用@ConfigurationProperties注解
首先需要在pom文件里引入依賴(lài)
<!-- 專(zhuān)門(mén)用于數(shù)據(jù)綁定的依賴(lài) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
其次,新建一個(gè)config的java文件蔑滓,比如
@Component
@ConfigurationProperties(prefix = "frank.springboot")
public class SelfConfig {
private String version;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
最后使用如下:
@Autowired
private SelfConfig selfConfig;
System.out.println(selfConfig.getVersion());