需要兩步驟
1、在類上添加註解@PropertySource,指定配置文件路徑,默認路徑下可以不添加此註解(application.properties中的配置信息讀取就不需要此註解,直接第二步)
classpath:con/myConfig.properties指的是src/main/resources目錄下con目錄下的myConfig.properties文件
@PropertySource({"classpath:config/my.properties","classpath:config/config.properties"}) public class TestController
2、在屬性名上添加@Value註解
@Value("${file.name}") private String fileName;