扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

springboot鎬庝箞鍔犺浇鎸囧畾閰嶇疆鏂囦欢

扬州沐宇科技
2024-01-16 20:18:54
springboot

Spring Boot鍙互閫氳繃@ConfigurationProperties娉ㄨВ鏉ュ姞杞芥寚瀹氱殑閰嶇疆鏂囦欢銆傚叿浣撴楠ゅ涓嬶細

  1. 鍦ㄩ」鐩殑resources鐩綍涓嬶紝鍒涘缓涓€涓厤缃枃浠讹紝姣斿application-dev.properties锛屽叾涓璬ev涓烘寚瀹氱殑鐜鍚嶇О銆?/p>

  2. 鍦⊿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
}
  1. 鍦╬om.xml鏂囦欢涓坊鍔犲涓嬩緷璧栵細
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
  1. 鍦╝pplication.properties鎴朼pplication.yml鏂囦欢涓缃?quot;spring.profiles.active"灞炴€т负鎸囧畾鐨勭幆澧冿紝姣斿"dev"銆?/li>
spring.profiles.active=dev

鎴栬€?/p>

spring:
  profiles:
    active: dev

杩欐牱锛孲pring Boot灏变細鏍规嵁鎸囧畾鐨勯厤缃枃浠跺姞杞藉搴旂殑閰嶇疆淇℃伅銆?/p>

扫码添加客服微信