![](/img/trans.png)
[英]Spring Boot with multiple datasources and external configuration, Spring JPA
[英]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 参考指南中的外部化配置)。
当解析属性(即@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/
,则搜索顺序如下:
file:./custom-config/
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 文件,我的应用程序用户存储在其中。
我的要求:
从以下位置加载文件(按此顺序)
我想出了以下解决方案:
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.