簡體   English   中英

如何根據彈簧配置文件加載屬性文件

[英]How to load property file based on spring profiles

如何創建項目架構以支持多種環境。 在spring的幫助下,每個環境都將具有來自不同屬性文件(例如(dev-propertfile,test-propertyFil,Production-propertyfile)的不同數據源

org.springframework.core.env.Environment;

將屬性文件放在與application.property相同的位置,並遵循命名約定application-{profile}.properties例如application-dev.propertiesapplication-test.propertiesapplication-prod.properties

並在application.properties設置spring.profiles.active=dev,test

對於Spring Boot應用程序,即使使用YAML文件也可以輕松工作

spring: 
  profiles: dev
  property: this is a dev env
---
spring: 
  profiles: prod
  property: this is a production env 
---

但是,對於Spring MVC應用程序,它需要做更多的工作。 看看這個鏈接

基本上涉及兩個步驟

  1. 在Servlet上下文中獲取Spring Profile

如果您在服務器上設置了配置文件,並希望它在您的應用程序中檢索它,則可以使用System.getProperty或System.getenv方法。 這是獲取配置文件並將其默認為本地配置文件(如果未找到配置文件)的代碼。

private static final String SPRING_PROFILES_ACTIVE = "SPRING_PROFILES_ACTIVE";
String profile;

/**
 * In local system getProperty() returns the profile correctly, however in docker getenv() return profile correctly
 * */
protected void setSpringProfile(ServletContext servletContext) {
if(null!= System.getenv(SPRING_PROFILES_ACTIVE)){
    profile=System.getenv(SPRING_PROFILES_ACTIVE);
}else if(null!= System.getProperty(SPRING_PROFILES_ACTIVE)){
    profile=System.getProperty(SPRING_PROFILES_ACTIVE);
}else{
    profile="local";
}
log.info("***** Profile configured  is  ****** "+ profile);

servletContext.setInitParameter("spring.profiles.active", profile);
}
  1. 要訪問application-dev.properties,現在說您將需要在類級別使用@Profile(“ dev”)

以下代碼將獲取application-dev.properties和common.properties

@Configuration
@Profile("dev")
public class DevPropertyReader {


    @Bean
    public static PropertyPlaceholderConfigurer properties() {
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    Resource[] resources = new ClassPathResource[] { new ClassPathResource("properties/common.properties"), new ClassPathResource("properties/application-dev.properties") };
    ppc.setLocations(resources);
    ppc.setIgnoreUnresolvablePlaceholders(true);
    return ppc;
    }
}

要訪問說application-prod.properties,您必須在類級別使用@Profile("prod") 可以在這里找到更多詳細信息

我將逐步介紹Spring引導應用程序的過程。

  1. /src/main/resources/application.properties內部,提到spring.profiles.active = dev (或Prod)
  2. 創建/src/main/resources/application-dev.properties並在此處提供自定義dev配置。
  3. 創建/src/main/resources/application-prod.properties並在此處提供自定義的prod配置。

跑。

看看Spring Profile 您將定義一組配置文件配置,例如測試,開發,生產。 然后,當您啟動應用程序時,您可以定義應用程序應該使用的配置文件。

這是一些使用方法的教程

這些家伙和您遇到了同樣的問題: 如何配置@ComponentScan dynamic?

我們想要一種根據 Spring MVC 項目中的環境(spring 配置文件)從application-<your_env>.properties文件加載不同屬性的方法,因此我們實現了類似這樣的配置類。

@Configuration
@PropertySource({ "classpath:application-${envTarget:dev}.properties" })
@Data
public class EnvironmentConfig {

    @Value("${files.s3.accessId:}")
    String s3AccessId;

    @Value("${files.s3.accessToken:}")
    String s3AccessToken;
    .
    .
    .
}

然后我們在需要使用它的類中加載EnvironmentConfig

在運行應用程序時,您只需要傳遞-DenvTarget=<your_env> ,它將從項目的src/resources文件夾中獲取application-<your_env>.properties文件。

在上面的代碼中,當沒有指定envTarget時,它將從application-dev.properties文件中加載值。

感謝 Karthikeyan Muthurangam 提出了這個聰明的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM