springboot鎬庝箞鍔犺浇鎸囧畾閰嶇疆鏂囦欢
Spring Boot鍙互閫氳繃@ConfigurationProperties娉ㄨВ鏉ュ姞杞芥寚瀹氱殑閰嶇疆鏂囦欢銆傚叿浣撴楠ゅ涓嬶細
-
鍦ㄩ」鐩殑resources鐩綍涓嬶紝鍒涘缓涓€涓厤缃枃浠讹紝姣斿application-dev.properties锛屽叾涓璬ev涓烘寚瀹氱殑鐜鍚嶇О銆?/p>
-
鍦⊿pring Boot椤圭洰鐨勯厤缃被涓婃坊鍔燖ConfigurationProperties娉ㄨВ锛屽苟璁剧疆prefix灞炴€т负閰嶇疆鏂囦欢涓殑鍓嶇紑锛屾瘮濡?quot;spring.datasource"銆傚悓鏃惰缃甽ocations灞炴€т负閰嶇疆鏂囦欢鐨勮矾寰勶紝姣斿"classpath:application-${spring.profiles.active}.properties"銆?/p>
绀轰緥浠g爜濡備笅锛?/p>
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
@PropertySource(value = {"classpath:application-${spring.profiles.active}.properties"})
public class DataSourceConfig {
private String url;
private String username;
private String password;
// getters and setters
}
- 鍦╬om.xml鏂囦欢涓坊鍔犲涓嬩緷璧栵細
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
- 鍦╝pplication.properties鎴朼pplication.yml鏂囦欢涓缃?quot;spring.profiles.active"灞炴€т负鎸囧畾鐨勭幆澧冿紝姣斿"dev"銆?/li>
spring.profiles.active=dev
鎴栬€?/p>
spring:
profiles:
active: dev
杩欐牱锛孲pring Boot灏变細鏍规嵁鎸囧畾鐨勯厤缃枃浠跺姞杞藉搴旂殑閰嶇疆淇℃伅銆?/p>
相关问答