簡體   English   中英

Spring Boot 和多個外部配置文件

[英]Spring Boot and multiple external configuration files

我有多個要從類路徑加載的屬性文件。 /src/main/resources下有一個默認設置,它是myapp.jar的一部分。 我的springcontext期望文件在類路徑上。 IE

<util:properties id="Job1Props"
    location="classpath:job1.properties"></util:properties>

<util:properties id="Job2Props"
    location="classpath:job2.properties"></util:properties>

我還需要使用外部集覆蓋這些屬性的選項。 我在cwd中有一個外部配置文件夾。 根據 spring boot doc config 文件夾應該在類路徑上。 但是從文檔中不清楚它是否只會從那里覆蓋application.properties或配置中的所有屬性。

當我測試它時,只有application.properties被拾取,其余屬性仍然從/src/main/resources中拾取。 我嘗試將它們作為逗號分隔列表提供給spring.config.location但默認設置仍未被覆蓋。

如何讓多個外部配置文件覆蓋默認配置文件?

作為解決方法,我目前使用了我通過命令行提供的app.config.location (應用程序特定屬性)。 IE

java -jar myapp.jar app.config.location=file:./config

我將我的applicationcontext上下文更改為

<util:properties id="Job1Props" location="{app.config.location}/job1.properties"></util:properties>

<util:properties id="Job2Props"
    location="{app.config.location}/job2.properties"></util:properties>

這就是我在加載應用程序時分離文件和類路徑的方式。
編輯:

//pseudo code

if (StringUtils.isBlank(app.config.location)) {
            System.setProperty(APP_CONFIG_LOCATION, "classpath:");
}

我真的不想使用上述解決方法,並讓 Spring 覆蓋類路徑上的所有外部配置文件,就像它對application.properties文件所做的那樣。

更新:由於 spring.config.location 現在的行為覆蓋了默認值而不是添加到它。 您需要使用spring.config.additional-location來保持默認值。 這是從 1.x 到 2.x 的行為變化


使用 Spring Boot 時,屬性按以下順序加載(請參閱 Spring Boot 參考指南中的外部化配置)。

  1. 命令行參數。
  2. Java 系統屬性 (System.getProperties())。
  3. 操作系統環境變量。
  4. 來自 java:comp/env 的 JNDI 屬性
  5. 僅具有隨機屬性的 RandomValuePropertySource。*。
  6. 打包 jar 之外的應用程序屬性(application.properties,包括 YAML 和配置文件變體)。
  7. 打包在 jar 中的應用程序屬性(application.properties 包括 YAML 和配置文件變體)。
  8. @Configuration 類上的 @PropertySource 注釋。
  9. 默認屬性(使用 SpringApplication.setDefaultProperties 指定)。

當解析屬性(即@Value("${myprop}")時,解析以相反的順序完成(所以從 9 開始)。

要添加不同的文件,您可以使用spring.config.location屬性,它采用逗號分隔的屬性文件列表或文件位置(目錄)。

-Dspring.config.location=your/config/dir/

上面的一個將添加一個目錄,該目錄將用於獲取application.properties文件。

-Dspring.config.location=classpath:job1.properties,classpath:job2.properties

這會將 2 個屬性文件添加到加載的文件中。

默認配置文件和位置在附加指定的spring.config.location之前加載,這意味着后者將始終覆蓋在早期設置中的屬性。 (另請參閱 Spring Boot 參考指南的這一部分)。

如果spring.config.location包含目錄(而不是文件),它們應該以 / 結尾(並且在加載之前將附加從spring.config.name生成的名稱)。 始終使用默認搜索路徑classpath:,classpath:/config,file:,file:config/ ,與spring.config.location的值無關。 這樣,您可以在application.properties (或您使用spring.config.name選擇的任何其他基本名稱)中為您的應用程序設置默認值,並在運行時使用不同的文件覆蓋它,保持默認值。

使用 Spring boot , spring.config.location 確實可以工作,只需提供逗號分隔的屬性文件。

看下面的代碼

@PropertySource(ignoreResourceNotFound=true,value="classpath:jdbc-${spring.profiles.active}.properties")
public class DBConfig{

     @Value("${jdbc.host}")
        private String jdbcHostName;
     }
}

可以將默認版本的 jdbc.properties 放入應用程序中。 外部版本可以設置在此。

java -jar target/myapp.jar --spring.config.location=classpath:file:///C:/Apps/springtest/jdbc.properties,classpath:file:///C:/Apps/springtest/jdbc-dev.properties

根據使用 spring.profiles.active 屬性設置的配置文件值,將獲取 jdbc.host 的值。 所以當(在 Windows 上)

set spring.profiles.active=dev

jdbc.host 將從 jdbc-dev.properties 中獲取值。

為了

set spring.profiles.active=default

jdbc.host 將從 jdbc.properties 中獲取值。

Spring boot 1.X 和 Spring Boot 2.X 沒有提供關於Externalized Configuration的相同選項和行為。

M. Deinum 的非常好的答案是指 Spring Boot 1 的特性。
我將在這里更新 Spring Boot 2。

環境屬性來源和順序

Spring Boot 2 使用了一個非常特殊的PropertySource順序,該順序旨在允許明智地覆蓋值。 屬性按以下順序考慮:

  • 主目錄上的 Devtools 全局設置屬性(當 devtools 處於活動狀態時為 ~/.spring-boot-devtools.properties)。

  • 測試中的@TestPropertySource注釋。

  • 測試中的@SpringBootTest#properties注釋屬性。 命令行參數。

  • 來自SPRING_APPLICATION_JSON的屬性(嵌入在環境變量或系統屬性中的內聯 JSON)。

  • ServletConfig初始化參數。

  • ServletContext初始化參數。

  • 來自java:comp/env的 JNDI 屬性。

  • Java 系統屬性 ( System.getProperties() )。

  • 操作系統環境變量。

  • 僅具有隨機屬性的RandomValuePropertySource 。*。

  • 打包 jar 之外的特定於配置文件的應用程序屬性( application-{profile}.properties和 YAML 變體)。

  • 打包在 jar 中的特定於配置文件的應用程序屬性( application-{profile}.properties和 YAML 變體)。

  • 打包 jar 之外的應用程序屬性( application.properties和 YAML 變體)。

  • 打包在 jar 中的應用程序屬性( application.properties和 YAML 變體)。

  • @Configuration類上的@PropertySource注釋。 默認屬性(通過設置SpringApplication.setDefaultProperties指定)。

要指定外部屬性文件,您應該對這些選項感興趣:

  • 打包 jar 之外的特定於配置文件的應用程序屬性( application-{profile}.properties和 YAML 變體)。

  • 打包 jar 之外的應用程序屬性( application.properties和 YAML 變體)。

  • @Configuration類上的@PropertySource注釋。 默認屬性(通過設置SpringApplication.setDefaultProperties指定)。

您可以只使用這 3 個選項中的一個,也可以根據您的要求將它們組合起來。
例如,對於非常簡單的情況,僅使用特定於配置文件的屬性就足夠了,但在其他情況下,您可能希望同時使用特定於配置文件的屬性、默認屬性和@PropertySource

application.properties 文件的默認位置

關於application.properties文件(和變體),默認情況下 Spring 會按以下順序加載它們並在環境中添加它們的屬性:

  • 當前目錄的 /config 子目錄

  • 當前目錄

  • 一個類路徑 /config 包

  • 類路徑根

從字面上看,更高的優先級是:
classpath:/,classpath:/config/,file:./,file:./config/

如何使用具有特定名稱的屬性文件?

默認位置並不總是足夠的:默認文件名( application.properties )等默認位置可能不適合。 此外,與 OP 問題一樣,您可能需要指定除application.properties (和變體)之外的多個配置文件。
所以spring.config.name是不夠的。

在這種情況下,您應該使用spring.config.location環境屬性(以逗號分隔的目錄位置或文件路徑列表)來提供顯式位置。
為了自由使用文件名模式,優先考慮文件路徑列表而不是目錄列表。
例如這樣做:

java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

這種方式是僅指定文件夾最冗長的方式,但它也是非常精細地指定我們的配置文件並清楚地記錄有效使用的屬性的方式。

spring.config.location 現在替換默認位置而不是添加到它們

在 Spring Boot 1 中, spring.config.location參數在 Spring 環境中添加了指定的位置。
但是從 Spring Boot 2 開始, spring.config.location將 Spring 使用的默認位置替換為 Spring 環境中的指定位置,如文檔中所述

當使用spring.config.location配置自定義配置位置時,它們會替換默認位置。 例如,如果spring.config.location配置了值classpath:/custom-config/ , file:./custom-config/ ,則搜索順序如下:

  1. file:./custom-config/

  2. classpath:custom-config/

spring.config.location現在是一種確保必須明確指定任何application.properties文件的方法。
對於不應該打包application.properties文件的 uber JAR,這是相當不錯的。

要在使用 Spring Boot 2 時保持spring.config.location的舊行為,您可以使用新的spring.config.additional-location屬性而不是spring.config.location ,它仍然會添加文檔中所述的位置:

或者,當使用spring.config.additional-location配置自定義配置位置時,除了默認位置之外,還會使用它們。


在實踐中

因此,假設在 OP 問題中,您有 2 個要指定的外部屬性文件和 1 個包含在 uber jar 中的屬性文件。

要僅使用您指定的配置文件:

-Dspring.config.location=classpath:/job1.properties,classpath:/job2.properties,classpath:/applications.properties   

要將配置文件添加到默認位置:

-Dspring.config.additional-location=classpath:/job1.properties,classpath:/job2.properties

classpath:/applications.properties在最后一個示例中不是必需的,因為默認位置具有該屬性,並且此處的默認位置不會被覆蓋而是被擴展。

看一下PropertyPlaceholderConfigurer,我覺得用起來比注解更清楚。

例如

@Configuration
public class PropertiesConfiguration {


    @Bean
    public PropertyPlaceholderConfigurer properties() {
        final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
//        ppc.setIgnoreUnresolvablePlaceholders(true);
        ppc.setIgnoreResourceNotFound(true);

        final List<Resource> resourceLst = new ArrayList<Resource>();

        resourceLst.add(new ClassPathResource("myapp_base.properties"));
        resourceLst.add(new FileSystemResource("/etc/myapp/overriding.propertie"));
        resourceLst.add(new ClassPathResource("myapp_test.properties"));
        resourceLst.add(new ClassPathResource("myapp_developer_overrides.properties")); // for Developer debugging.

        ppc.setLocations(resourceLst.toArray(new Resource[]{}));

        return ppc;
    }

這是使用彈簧靴的一種簡單方法

測試類.java

@Configuration
@Profile("one")
@PropertySource("file:/{selected location}/app.properties")
public class TestClass {

    @Autowired
    Environment env;

    @Bean
    public boolean test() {
        System.out.println(env.getProperty("test.one"));
        return true;
    }
}

app.properties上下文,在您選擇的位置

test.one = 1234

你的Spring Boot 應用程序

@SpringBootApplication

public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(testApplication.class, args);
    }
}

和預定義的application.properties上下文

spring.profiles.active = one

您可以編寫任意數量的配置類並通過設置spring.profiles.active = profile name/names {以逗號分隔}來啟用/禁用它們

如您所見,spring boot 很棒,只是需要一些時間來熟悉,值得一提的是,您也可以在字段上使用 @Value

@Value("${test.one}")
String str;

我有同樣的問題。 我希望能夠在啟動時使用外部文件覆蓋內部配置文件,類似於 Spring Boot application.properties 檢測。 在我的情況下,它是一個 user.properties 文件,我的應用程序用戶存儲在其中。

我的要求:

從以下位置加載文件(按此順序)

  1. 類路徑
  2. 當前目錄的/config子目錄。
  3. 當前目錄
  4. 從目錄或啟動時命令行參數給出的文件位置

我想出了以下解決方案:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Properties;

import static java.util.Arrays.stream;

@Configuration
public class PropertiesConfig {

    private static final Logger LOG = LoggerFactory.getLogger(PropertiesConfig.class);

    private final static String PROPERTIES_FILENAME = "user.properties";

    @Value("${properties.location:}")
    private String propertiesLocation;

    @Bean
    Properties userProperties() throws IOException {
        final Resource[] possiblePropertiesResources = {
                new ClassPathResource(PROPERTIES_FILENAME),
                new PathResource("config/" + PROPERTIES_FILENAME),
                new PathResource(PROPERTIES_FILENAME),
                new PathResource(getCustomPath())
        };
        // Find the last existing properties location to emulate spring boot application.properties discovery
        final Resource propertiesResource = stream(possiblePropertiesResources)
                .filter(Resource::exists)
                .reduce((previous, current) -> current)
                .get();
        final Properties userProperties = new Properties();

        userProperties.load(propertiesResource.getInputStream());

        LOG.info("Using {} as user resource", propertiesResource);

        return userProperties;
    }

    private String getCustomPath() {
        return propertiesLocation.endsWith(".properties") ? propertiesLocation : propertiesLocation + PROPERTIES_FILENAME;
    }

}

現在應用程序使用類路徑資源,但也在其他給定位置檢查資源。 最后存在的資源將被挑選和使用。 我可以使用 java -jar myapp.jar --properties.location=/directory/myproperties.properties 啟動我的應用程序,以使用漂浮我的船的屬性位置。

這里有一個重要的細節:在 @Value 注釋中使用空字符串作為 properties.location 的默認值,以避免在未設置屬性時出錯。

properties.location 的約定是:使用目錄或屬性文件的路徑作為 properties.location。

如果您只想覆蓋特定屬性,則可以使用帶有 setIgnoreResourceNotFound(true) 的 PropertiesFactoryBean,並將資源數組設置為位置。

我確信這個解決方案可以擴展到處理多個文件......

編輯

這是我的多個文件的解決方案:) 像以前一樣,這可以與 PropertiesFactoryBean 結合使用。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toMap;

@Configuration
class PropertiesConfig {

    private final static Logger LOG = LoggerFactory.getLogger(PropertiesConfig.class);
    private final static String[] PROPERTIES_FILENAMES = {"job1.properties", "job2.properties", "job3.properties"};

    @Value("${properties.location:}")
    private String propertiesLocation;

    @Bean
    Map<String, Properties> myProperties() {
        return stream(PROPERTIES_FILENAMES)
                .collect(toMap(filename -> filename, this::loadProperties));
    }

    private Properties loadProperties(final String filename) {
        final Resource[] possiblePropertiesResources = {
                new ClassPathResource(filename),
                new PathResource("config/" + filename),
                new PathResource(filename),
                new PathResource(getCustomPath(filename))
        };
        final Resource resource = stream(possiblePropertiesResources)
                .filter(Resource::exists)
                .reduce((previous, current) -> current)
                .get();
        final Properties properties = new Properties();

        try {
            properties.load(resource.getInputStream());
        } catch(final IOException exception) {
            throw new RuntimeException(exception);
        }

        LOG.info("Using {} as user resource", resource);

        return properties;
    }

    private String getCustomPath(final String filename) {
        return propertiesLocation.endsWith(".properties") ? propertiesLocation : propertiesLocation + filename;
    }

}

spring boot 允許我們為不同的環境編寫不同的配置文件,例如我們可以為生產、qa 和本地環境有單獨的屬性文件

根據我的本地機器配置的 application-local.properties 文件是

spring.profiles.active=local

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=users
spring.data.mongodb.username=humble_freak
spring.data.mongodb.password=freakone

spring.rabbitmq.host=localhost
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.port=5672

rabbitmq.publish=true

同樣,我們可以編寫任意數量的 application-prod.properties 和 application-qa.properties 屬性文件

然后編寫一些腳本來啟動不同環境的應用程序,例如

mvn spring-boot:run -Drun.profiles=local
mvn spring-boot:run -Drun.profiles=qa
mvn spring-boot:run -Drun.profiles=prod

我剛剛遇到了類似的問題,終於找到了原因:application.properties 文件的所有權和 rwx 屬性錯誤。 因此,當 tomcat 啟動時,application.properties 文件位於正確的位置,但歸另一個用戶所有:

$ chmod 766 application.properties

$ chown tomcat application.properties

@mxsb 解決方案的修改版本,允許我們定義多個文件,在我的情況下,這些是 yml 文件。

在我的 application-dev.yml 中,我添加了這個配置,允許我注入其中包含 -dev.yml 的所有 yml。 這也可以是特定文件的列表。 “類路徑:/test/test.yml,類路徑:/test2/test.yml”

application:
  properties:
    locations: "classpath*:/**/*-dev.yml"

這有助於獲取屬性映射。

@Configuration

public class PropertiesConfig {

private final static Logger LOG = LoggerFactory.getLogger(PropertiesConfig.class);

@Value("${application.properties.locations}")
private String[] locations;

@Autowired
private ResourceLoader rl;

@Bean
Map<String, Properties> myProperties() {
    return stream(locations)
            .collect(toMap(filename -> filename, this::loadProperties));
}

private Properties loadProperties(final String filename) {

    YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
    try {
        final Resource[] possiblePropertiesResources = ResourcePatternUtils.getResourcePatternResolver(rl).getResources(filename);
        final Properties properties = new Properties();
        stream(possiblePropertiesResources)
                .filter(Resource::exists)
                .map(resource1 -> {
                    try {
                        return loader.load(resource1.getFilename(), resource1);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }).flatMap(l -> l.stream())
                .forEach(propertySource -> {
                    Map source = ((MapPropertySource) propertySource).getSource();
                    properties.putAll(source);
                });

        return properties;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
}

但是,如果像我的情況一樣,我想必須為每個配置文件拆分 yml 文件並加載它們,然后在 bean 初始化之前將其直接注入到 spring 配置中。

config
    - application.yml
    - application-dev.yml
    - application-prod.yml
management
    - management-dev.yml
    - management-prod.yml

...你明白了

組件略有不同

@Component
public class PropertiesConfigurer extends     PropertySourcesPlaceholderConfigurer
    implements EnvironmentAware, InitializingBean {

private final static Logger LOG = LoggerFactory.getLogger(PropertiesConfigurer.class);

private String[] locations;

@Autowired
private ResourceLoader rl;
private Environment environment;

@Override
public void setEnvironment(Environment environment) {
    // save off Environment for later use
    this.environment = environment;
    super.setEnvironment(environment);
}

@Override
public void afterPropertiesSet() throws Exception {
    // Copy property sources to Environment
    MutablePropertySources envPropSources = ((ConfigurableEnvironment) environment).getPropertySources();
    envPropSources.forEach(propertySource -> {
        if (propertySource.containsProperty("application.properties.locations")) {
            locations = ((String) propertySource.getProperty("application.properties.locations")).split(",");
            stream(locations).forEach(filename -> loadProperties(filename).forEach(source ->{
                envPropSources.addFirst(source);
            }));
        }
    });
}


private List<PropertySource> loadProperties(final String filename) {
    YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
    try {
        final Resource[] possiblePropertiesResources = ResourcePatternUtils.getResourcePatternResolver(rl).getResources(filename);
        final Properties properties = new Properties();
        return stream(possiblePropertiesResources)
                .filter(Resource::exists)
                .map(resource1 -> {
                    try {
                        return loader.load(resource1.getFilename(), resource1);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }).flatMap(l -> l.stream())
                .collect(Collectors.toList());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

}

如果要覆蓋 application.properties 文件中指定的值,可以在運行應用程序時更改活動配置文件並為配置文件創建應用程序屬性文件。 因此,例如,讓我們指定活動配置文件“覆蓋”,然后假設您已經在 /tmp 下創建了名為“application-override.properties”的新應用程序屬性文件,那么您可以運行

java -jar yourApp.jar --spring.profiles.active="override" --spring.config.location="file:/tmp/,classpath:/" 

spring.config.location 下指定的值以相反的順序進行評估。 因此,在我的示例中,首先評估 classpat,然后評估文件值。

如果 jar 文件和“application-override.properties”文件在當前目錄中,您實際上可以簡單地使用

java -jar yourApp.jar --spring.profiles.active="override"

因為 Spring Boot 會為你找到屬性文件

我發現這是一個有用的模式:

@RunWith(SpringRunner)
@SpringBootTest(classes = [ TestConfiguration, MyApplication ],
        properties = [
                "spring.config.name=application-MyTest_LowerImportance,application-MyTest_MostImportant"
                ,"debug=true", "trace=true"
        ]
)

在這里,我們覆蓋使用“application.yml”來使用“application-MyTest_LowerImportance.yml”和“application-MyTest_MostImportant.yml”
(Spring 也會查找 .properties 文件)

作為額外獎勵還包括調試和跟蹤設置,位於單獨的行上,因此您可以在需要時將它們注釋掉;]

調試/跟蹤非常有用,因為 Spring 會轉儲它加載的所有文件的名稱以及它嘗試加載的文件的名稱。
您將在運行時在控制台中看到這樣的行:

TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./config/application-MyTest_MostImportant.properties' (file:./config/application-MyTest_MostImportant.properties) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./config/application-MyTest_MostImportant.xml' (file:./config/application-MyTest_MostImportant.xml) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./config/application-MyTest_MostImportant.yml' (file:./config/application-MyTest_MostImportant.yml) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./config/application-MyTest_MostImportant.yaml' (file:./config/application-MyTest_MostImportant.yaml) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./config/application-MyTest_LowerImportance.properties' (file:./config/application-MyTest_LowerImportance.properties) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./config/application-MyTest_LowerImportance.xml' (file:./config/application-MyTest_LowerImportance.xml) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./config/application-MyTest_LowerImportance.yml' (file:./config/application-MyTest_LowerImportance.yml) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./config/application-MyTest_LowerImportance.yaml' (file:./config/application-MyTest_LowerImportance.yaml) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./application-MyTest_MostImportant.properties' (file:./application-MyTest_MostImportant.properties) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./application-MyTest_MostImportant.xml' (file:./application-MyTest_MostImportant.xml) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./application-MyTest_MostImportant.yml' (file:./application-MyTest_MostImportant.yml) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./application-MyTest_MostImportant.yaml' (file:./application-MyTest_MostImportant.yaml) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./application-MyTest_LowerImportance.properties' (file:./application-MyTest_LowerImportance.properties) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./application-MyTest_LowerImportance.xml' (file:./application-MyTest_LowerImportance.xml) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./application-MyTest_LowerImportance.yml' (file:./application-MyTest_LowerImportance.yml) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./application-MyTest_LowerImportance.yaml' (file:./application-MyTest_LowerImportance.yaml) resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/config/application-MyTest_MostImportant.properties' resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/config/application-MyTest_MostImportant.xml' resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/config/application-MyTest_MostImportant.yml' resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/config/application-MyTest_MostImportant.yaml' resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/config/application-MyTest_LowerImportance.properties' resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/config/application-MyTest_LowerImportance.xml' resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/config/application-MyTest_LowerImportance.yml' resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/config/application-MyTest_LowerImportance.yaml' resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/application-MyTest_MostImportant.properties' resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/application-MyTest_MostImportant.xml' resource not found
DEBUG 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file 'file:/Users/xxx/dev/myproject/target/test-classes/application-MyTest_MostImportant.yml' (classpath:/application-MyTest_MostImportant.yml)
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/application-MyTest_MostImportant.yaml' resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/application-MyTest_LowerImportance.properties' resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/application-MyTest_LowerImportance.xml' resource not found
DEBUG 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file 'file:/Users/xxx/dev/myproject/target/test-classes/application-MyTest_LowerImportance.yml' (classpath:/application-MyTest_LowerImportance.yml)
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/application-MyTest_LowerImportance.yaml' resource not found
TRACE 93941 --- [   main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./config/application-MyTest_MostImportant-test.properties' (file:./config/application-MyTest_MostImportant-test.properties) resource not found

如果您的資源文件夾中有 application-intranet.properties,您可以像這樣使用-Dspring.profiles.active=intranet 注意:內網是我的特定文件名,你的應該不同

在試圖解決這個問題時,我遇到了很多問題。 這是我的設置,

開發環境:Windows 10,Java:1.8.0_25,Spring Boot:2.0.3.RELEASE,Spring:5.0.7.RELEASE

我發現 spring 堅持“合理的配置默認值”的概念。 這意味着,您必須將所有屬性文件作為戰爭文件的一部分。 在那里,您可以使用“--spring.config.additional-location”命令行屬性覆蓋它們以指向外部屬性文件。 但是如果屬性文件不是原始戰爭文件的一部分,這將不起作用。

演示代碼: https ://github.com/gselvara/spring-boot-property-demo/tree/master

暫無
暫無

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

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