一.在resources目錄下添加自定義的test.properties文件
test.properties內容如下:
host=127.0.0.1
port=8080
二.編寫一個讀取配置文件內容的類
@Configuration @PropertySource("classpath:test.properties")//注意路徑 public class TestConfig { @Value("${host}") private String host; @Value("${port}") private int port; public String getHost() { return host; } public int getPort() { return port; } }
三.配置文件獲取
@Autowired private TestConfig testConfig; public void test() { System.out.peintln(testConfig.getHost()) }