springboot鎬庝箞鍔犺浇閰嶇疆鏂囦欢
Spring Boot閫氳繃@PropertySource
娉ㄨВ鏉ュ姞杞介厤缃枃浠躲€?/p>
- 鍦?code>@SpringBootApplication娉ㄨВ鎵€鍦ㄧ殑绫讳笂娣诲姞
@PropertySource
娉ㄨВ锛屾寚瀹氳鍔犺浇鐨勯厤缃枃浠惰矾寰勩€備緥濡傦紝鍔犺浇鍚嶄负application.properties
鐨勯厤缃枃浠讹紝浠g爜濡備笅锛?/li>
@SpringBootApplication
@PropertySource("classpath:application.properties")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
- 鍦?code>application.properties閰嶇疆鏂囦欢涓紝娣诲姞闇€瑕佺殑閰嶇疆椤广€備緥濡傦細
server.port=8080
- 鍦ㄩ渶瑕佽幏鍙栭厤缃」鐨勫湴鏂癸紝浣跨敤
@Value
娉ㄨВ鏉ユ敞鍏ラ厤缃」鐨勫€笺€備緥濡傦紝鍦ㄤ竴涓?code>@Component鏍囨敞鐨勭被涓幏鍙?code>server.port閰嶇疆椤圭殑鍊硷細
@Component
public class MyComponent {
@Value("${server.port}")
private String port;
// ...
}
浠ヤ笂灏辨槸浣跨敤Spring Boot鍔犺浇閰嶇疆鏂囦欢鐨勬柟娉曘€傚湪閰嶇疆鏂囦欢涓坊鍔犻渶瑕佺殑閰嶇疆椤癸紝骞跺湪闇€瑕佷娇鐢ㄧ殑鍦版柟閫氳繃娉ㄨВ鏉ヨ幏鍙栭厤缃」鐨勫€笺€?/p>
相关问答