简体   繁体   English

spring.profiles.active不能从命令行使用,但可以在IDE中使用

[英]spring.profiles.active not being honored from command line, but works in IDE

At a bit of a loss here. 这里有点茫然。 I have a sample application/proof-of-concept that I am trying to get to work with the following configuration: 我有一个示例应用程序/概念证明,我正在尝试使用以下配置:

Layer 1: library that utilizes Spring components, to be hidden away from end user libraries/applications. 第1层:利用Spring组件的库对于最终用户库/应用程序而言是隐藏的。

Layer 2: library that pulls in the aforementioned library, but is otherwise non-Spring implemented. 第2层:引入上述库的库,但非Spring实施。 Smart enough to auto-establish the Spring context, but hide the details of this from users of the library. 足够智能,可以自动建立Spring上下文,但是对库用户隐藏了此细节。

Layer 3: application that uses the "middleware" library in layer 2, also non-Spring aware. 第3层:使用第2层中的“中间件”库的应用程序,也不具备Spring意识。

I have all of this working as I expect, even being able to set the active profile (spring.profiles.active) and have the correct properties file be picked up from the classpath of the Layer 3 application. 我已经按照预期工作了,甚至能够设置活动配置文件(spring.profiles.active)并从第3层应用程序的类路径中获取正确的属性文件。 Here's the oddity: 奇怪的是:

If I run the application from my IDE (IntelliJ) and have it pass the active profile on the command line, all works as expected. 如果我从我的IDE(IntelliJ)运行该应用程序,并使其在命令行上通过了活动配置文件,则所有操作均按预期进行。 The values I load from my "production" properties file override those in the default properties file. 我从“生产”属性文件加载的值将覆盖默认属性文件中的值。 That is, value of key X in application-production.properties overrides the value of X in application.properties . 也就是说,在关键的X值application-production.properties覆盖中的X值application.properties

All good, right? 一切都好吧? Ok, try to run the jar from the command line doing the same thing ( java -Dspring.profiles.active=production -jar my-app-1.0-SNAPSHOT-full.jar ). 好的,尝试从命令行执行相同的操作来运行jar( java -Dspring.profiles.active=production -jar my-app-1.0-SNAPSHOT-full.jar )。 I see in the debug log that the "production" properties file is picked up, but the default file is being given precedence. 我在调试日志中看到选择了“生产”属性文件,但是默认文件被赋予了优先级。 That is, the value in application.properties is being used, even though the Spring context clearly states that "production" is active, and it finds and loads application-production.properties just fine. 也就是说,即使Spring上下文明确指出“生产”处于活动状态,它也会使用application.properties的值,并且找到并加载application-production.properties也很好。

Eh, WAT?! 恩,WAT ?!

Has anyone else experienced this, and if so, any workaround? 其他人有没有遇到过这种情况? I'm pulling in Spring IO Platform Brussels-SR5, which includes version 4.3.11.RELEASE of spring-core, spring-context, et al. 我正在使用Spring IO Platform Brussels-SR5,其中包括4.3.11版.spring-core,spring-context等的发布。 The jar for the application is a shaded jar that contains all dependencies. 该应用程序的jar是一个包含所有依赖项的阴影jar。 The two library jars are not shaded. 这两个库罐未着色。

EDIT 编辑

Log is rather long ... going to try and snip to the relevant parts ... 日志相当长...打算尝试剪到相关部分...

william@jessie:/mnt/builds/william/src/mixed-spring-legacy-app$ java -Dspring.profiles.active=production -jar example-app/target/example-app-1.0-SNAPSHOT-full.jar 
20:20:18.726 [main] DEBUG org.springframework.core.env.StandardEnvironment - Adding PropertySource 'systemProperties' with lowest search precedence
20:20:18.732 [main] DEBUG org.springframework.core.env.StandardEnvironment - Adding PropertySource 'systemEnvironment' with lowest search precedence
20:20:18.733 [main] DEBUG org.springframework.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [MapPropertySource@20557198 {name='systemProperties', properties={java.runtime.name=OpenJDK Runtime Environment, sun.boot.library.path=/usr/lib/jvm/java-8-openjdk-i386/jre/lib/i386, java.vm.version=25.141-b15, java.vm.vendor=Oracle Corporation, java.vendor.url=http://java.oracle.com/, path.separator=:, java.vm.name=OpenJDK Server VM, file.encoding.pkg=sun.io, user.country=US, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=unknown, java.vm.specification.name=Java Virtual Machine Specification, user.dir=/mnt/builds/william/src/mixed-spring-legacy-app, java.runtime.version=1.8.0_141-8u141-b15-1~deb9u1-b15, java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment, java.endorsed.dirs=/usr/lib/jvm/java-8-openjdk-i386/jre/lib/endorsed, os.arch=i386, spring.profiles.active=production, java.io.tmpdir=/tmp, line.separator=
, java.vm.specification.vendor=Oracle Corporation, os.name=Linux, sun.jnu.encoding=UTF-8, java.library.path=/usr/java/packages/lib/i386:/usr/lib/i386-linux-gnu/jni:/lib/i386-linux-gnu:/usr/lib/i386-linux-gnu:/usr/lib/jni:/lib:/usr/lib, java.specification.name=Java Platform API Specification, java.class.version=52.0, sun.management.compiler=HotSpot Tiered Compilers, os.version=4.9.0-3-686-pae, user.home=/home/william, user.timezone=America/New_York, java.awt.printerjob=sun.print.PSPrinterJob, file.encoding=UTF-8, java.specification.version=1.8, java.class.path=example-app/target/example-app-1.0-SNAPSHOT-full.jar, user.name=william, java.vm.specification.version=1.8, sun.java.command=example-app/target/example-app-1.0-SNAPSHOT-full.jar, java.home=/usr/lib/jvm/java-8-openjdk-i386/jre, sun.arch.data.model=32, user.language=en, java.specification.vendor=Oracle Corporation, awt.toolkit=sun.awt.X11.XToolkit, java.vm.info=mixed mode, java.version=1.8.0_141, java.ext.dirs=/usr/lib/jvm/java-8-openjdk-i386/jre/lib/ext:/usr/java/packages/lib/ext, sun.boot.class.path=/usr/lib/jvm/java-8-openjdk-i386/jre/lib/resources.jar:/usr/lib/jvm/java-8-openjdk-i386/jre/lib/rt.jar:/usr/lib/jvm/java-8-openjdk-i386/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-8-openjdk-i386/jre/lib/jsse.jar:/usr/lib/jvm/java-8-openjdk-i386/jre/lib/jce.jar:/usr/lib/jvm/java-8-openjdk-i386/jre/lib/charsets.jar:/usr/lib/jvm/java-8-openjdk-i386/jre/lib/jfr.jar:/usr/lib/jvm/java-8-openjdk-i386/jre/classes, java.vendor=Oracle Corporation, file.separator=/, java.vendor.url.bug=http://bugreport.sun.com/bugreport/, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.cpu.isalist=}}, SystemEnvironmentPropertySource@5592464 {name='systemEnvironment', properties={ .. truncated ..}}]
20:20:18.765 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved classpath location [tech/chihuahua/examples/] to resources [URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/]]
20:20:18.767 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Looking for matching resources in jar file [file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar]
20:20:18.786 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved location pattern [classpath*:tech/chihuahua/examples/**/*.class] to resources [URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/client/GreetingClient.class], URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/client/GreetingClientImpl.class], URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/client/GreetingClientFactory.class], URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryProductionContext.class], URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryStagingContext.class], URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryDefaultContext.class], URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryIntegrationTestContext.class], URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryProductionContext$ProductionProperties.class], URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryIntegrationTestContext$IntegrationTestProperties.class], URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryStagingContext$StagingProperties.class], URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryDefaultContext$DefaultProperties.class], URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/Profiles.class], URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/LibraryContextProvider.class], URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/service/GreetingService.class]]
20:20:18.825 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryProductionContext.class]
20:20:18.825 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryStagingContext.class]
20:20:18.831 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryDefaultContext.class]
20:20:18.832 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryIntegrationTestContext.class]
20:20:18.840 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'spring.profiles.active' in PropertySource 'systemProperties' with value of type String
20:20:18.840 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryProductionContext$ProductionProperties.class]
20:20:18.842 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/config/LibraryDefaultContext$DefaultProperties.class]
20:20:18.845 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/service/GreetingService.class]
20:20:18.858 [main] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@16f64b1: startup date [Thu Oct 05 20:20:18 EDT 2017]; root of context hierarchy
20:20:18.858 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Bean factory for org.springframework.context.annotation.AnnotationConfigApplicationContext@16f64b1: org.springframework.beans.factory.support.DefaultListableBeanFactory@1629756: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,libraryProductionContext,libraryStagingContext,libraryDefaultContext,libraryIntegrationTestContext,libraryProductionContext.ProductionProperties,libraryDefaultContext.DefaultProperties,greetingService]; root of factory hierarchy
20:20:18.883 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
20:20:18.883 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
20:20:18.902 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
20:20:18.904 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
20:20:18.930 [main] DEBUG org.springframework.core.env.StandardEnvironment - Adding PropertySource 'class path resource [application-production.properties]' with lowest search precedence
20:20:18.936 [main] DEBUG org.springframework.core.env.StandardEnvironment - Adding PropertySource 'class path resource [application.properties]' with search precedence immediately higher than 'class path resource [application-production.properties]'
20:20:18.938 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved classpath location [tech/chihuahua/examples/] to resources [URL [jar:file:/mnt/builds/william/src/mixed-spring-legacy-app/example-app/target/example-app-1.0-SNAPSHOT-full.jar!/tech/chihuahua/examples/]]

So, you can see here that the profile-specific properties file is being set to lower priority than the catch-all properties file. 因此,您可以在此处看到特定于配置文件的属性文件的优先级低于全部捕获属性文件。 Also, 也,

20:20:19.128 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'greetingService' to allow for resolving potential circular references
20:20:19.128 [main] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected element of bean 'greetingService': AutowiredMethodElement for public void tech.chihuahua.examples.service.GreetingService.setEnvironment(org.springframework.core.env.Environment)
20:20:19.130 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'environment'
20:20:19.130 [main] DEBUG org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - Autowiring by type from bean name 'greetingService' to bean named 'environment'
    Active profile: production
20:20:19.179 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'tech.chihuahua.examples.greeting.format' in PropertySource 'class path resource [application.properties]' with value of type String
    tech.chihuahua.examples.greeting.format set to 'Hi, %s!'
20:20:19.179 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'greetingService'
20:20:19.179 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'propertySourcesPlaceholderConfigurer'
20:20:19.180 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
20:20:19.210 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@1afa13b]
20:20:19.210 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
20:20:19.212 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
20:20:19.214 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'greetingService'
20:20:19.214 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'tech.chihuahua.examples.greeting.format' in PropertySource 'class path resource [application.properties]' with value of type String
Hi, Jack!

Where I actually invoke the bean, I added code to show what profile is active, and what the property value contains. 在实际调用Bean的地方,添加了代码以显示活动的概要文件以及属性值包含的内容。

As I said previously, this works correctly when running from my IDE, but not from the jar file. 就像我之前说过的那样,当从我的IDE(而不是从jar文件)运行时,这可以正常工作。

EDIT #2 编辑#2

So I tried removing the properties files from the jar and running with them in the current directory. 因此,我尝试从jar中删除属性文件,并在当前目录中运行它们。 Same result. 结果相同。 Dumped the jar out to a folder, added the properties files to that folder ... works like it is supposed to. 将罐子倾倒到一个文件夹中,然后将属性文件添加到该文件夹​​中……效果应该像预期的那样。 So it is definitely something to do with the jar in the mix. 因此,这与混合罐绝对有关。

So I finally came up with a workaround to this. 所以我终于想出了一个解决方法。 As for why it was working with all of the classes/properties files in a folder and not when inside the jar, the best answer I can give is "purple, because aliens don't wear hats." 至于为什么它使用文件夹中的所有class / properties文件而不是在Jar中时,我能给出的最佳答案是“紫色,因为外星人不戴帽子”。

At any rate, what I had to do was to add another "profiled" properties class that is specific for the "default" profile. 无论如何,我要做的就是添加另一个专门针对“默认”配置文件的“配置文件”属性类。 That would receive whatever was in the "application-default.properties" file, and would only be activated if no other profiles were activated. 这将接收“ application-default.properties”文件中的内容,并且仅在没有其他配置文件被激活的情况下才被激活。 The "application.properties" file should contain no data that would be overridden based on active profile. “ application.properties”文件不应包含基于活动配置文件将被覆盖的数据。

Code example for how this works ... 有关如何工作的代码示例...

Catch-all/root configuration 全部捕获/根配置

package tech.chihuahua.examples.config;

import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import tech.chihuahua.examples.service.GreetingService;

@Configuration
@ComponentScan("tech.chihuahua.examples")
public class LibraryContext {

    @Configuration
    @PropertySources({
            @PropertySource(
                    value = "${spring.config.location}/application.properties",
                    ignoreResourceNotFound = true),
            @PropertySource(
                    value = "classpath:application.properties",
                    ignoreResourceNotFound = true)
    })
    static class ApplicationProperties {}

    @Bean
    GreetingService greetingService() {
        return new GreetingService();
    }

    @Bean
    static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

Defaults, for when no profile is active 默认值,当没有配置文件处于活动状态时

package tech.chihuahua.examples.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Configuration
public class LibraryDefaultContext {

    @Profile("default")
    @Configuration
    @PropertySources({
            @PropertySource(
                    value = "${spring.config.location}/application-default.properties",
                    ignoreResourceNotFound = true),
            @PropertySource(
                    value = "classpath:application-default.properties",
                    ignoreResourceNotFound = true)
    })
    static class DefaultProperties {}

}

Production profile active 生产资料有效

package tech.chihuahua.examples.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import tech.chihuahua.examples.Profiles;

@Configuration
public class LibraryProductionContext {

    @Profile(Profiles.PRODUCTION)
    @Configuration
    @PropertySources({
            @PropertySource(
                    value = "${spring.config.location}/application-production.properties",
                    ignoreResourceNotFound = true),
            @PropertySource(
                    value = "classpath:application-production.properties",
                    ignoreResourceNotFound = true)
    })
    static class ProductionProperties {}

}

Staging profile active 暂存配置文件已激活

package tech.chihuahua.examples.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import tech.chihuahua.examples.Profiles;

@Configuration
public class LibraryStagingContext {

    @Profile(Profiles.STAGING)
    @Configuration
    @PropertySources({
            @PropertySource(
                    value = "${spring.config.location}/application-staging.properties",
                    ignoreResourceNotFound = true),
            @PropertySource(
                    value = "classpath:application-staging.properties",
                    ignoreResourceNotFound = true)
    })
    static class StagingProperties {}

}

With all that in place, build the uber-jar. 一切就绪后,构建uber-jar。 Include the properties files in the jar, if so desired, or make sure they are available in a folder on your machine (eg /etc/my-app). 如果需要,将属性文件包括在jar中,或确保它们在您计算机上的文件夹(例如/ etc / my-app)中可用。

# The general application configuration settings go here.
/etc/my-app/application.properties

# The default configuration settings go here
# (e.g. configure the application to use an in-memory database)
/etc/my-app/application-default.properties

# The production configuration settings go here
# (e.g. configure the application to use your production database)
/etc/my-app/application-production.properties

# The staging configuration settings go here
# (e.g. configure the application to use a staging/test database)
/etc/my-app/application-staging.properties

Execute the app with the default settings 使用默认设置执行应用

Spring will automatically set the "default" profile to be active if no other profiles are specified. 如果未指定其他配置文件,Spring会自动将“默认”配置文件设置为活动状态。 So here, the "application-default.properties" file will be picked up, and its configuration will be used. 因此,这里将选择“ application-default.properties”文件,并使用其配置。

# Using the properties files in the classpath in the jar
java -jar my-app-1.0-SNAPSHOT-full.jar

# Using the properties files on the file system
java -Dspring.config.location=file:///etc/my-app \
     -jar my-app-1.0-SNAPSHOT-full.jar

Execute the app with the production profile active 在生产配置文件处于活动状态的情况下执行应用

By enabling the "production" profile, the "default" profile will not be activated. 通过启用“生产”配置文件,将不会激活“默认”配置文件。 Thus, "application-default.properties" is ignored, and "application-production.properties" is loaded. 因此,将忽略“ application-default.properties”,并加载“ application-production.properties”。

# Using the properties files in the classpath in the jar
java -Dspring.profiles.active=production \
     -jar my-app-1.0-SNAPSHOT-full.jar

# Using the properties files on the file system
java -Dspring.profiles.active=production \
     -Dspring.config.location=file:///etc/my-app \
     -jar my-app-1.0-SNAPSHOT-full.jar

Execute the app with the staging profile active 在登台配置文件处于活动状态的情况下执行应用

Same as previous, just using the "staging" profile instead of "production". 与以前相同,只是使用“登台”配置文件而不是“生产”。

# Using the properties files in the classpath in the jar
java -Dspring.profiles.active=staging \
     -jar my-app-1.0-SNAPSHOT-full.jar

# Using the properties files on the file system
java -Dspring.profiles.active=staging \
     -Dspring.config.location=file:///etc/my-app \
     -jar my-app-1.0-SNAPSHOT-full.jar

Hoping this helps save someone time and aggravation. 希望这有助于节省某人的时间和精力。

Wrap the property name in quotes as given below and try. 按如下所示将属性名称括在引号中,然后尝试。 This should work: 这应该工作:

java -D"spring.profiles.active"=production -jar example-app/target/example-app-1.0-SNAPSHOT-full.jar 

Please confirm if the suggested change solve the issue. 请确认建议的更改是否解决了问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot的多个配置文件可在IDE中使用,但不能从命令行使用 - Spring Boot multiple profiles works in IDE but not from command line spring.profiles.active 不兑现 - spring.profiles.active not honoured Gradle bootRun 不适用于 SPRING_PROFILES_ACTIVE 但适用于 spring.profiles.active - Gradle bootRun not working with SPRING_PROFILES_ACTIVE but works with spring.profiles.active spring.profiles.active 在springboot应用程序中不起作用 - spring.profiles.active is not working in springboot application "无法解析值“classpath:\/ldap-${spring.profiles.active}.properties”中的占位符“spring.profiles.active”" - Could not resolve placeholder 'spring.profiles.active' in value "classpath:/ldap-${spring.profiles.active}.properties" 为什么 spring.profiles.active 只读取部分配置文件? - Why does spring.profiles.active only read some profiles? 使用 Gradle 从 Spring Boot 中的环境变量设置 spring.profiles.active - Setting spring.profiles.active from an environment variable in Spring Boot with Gradle Spring 引导获取 static 块中的 spring.profiles.active 值 - Spring Boot get spring.profiles.active value in a static block 从位置“类路径资源 [application-dev.yml]”导入的属性“spring.profiles.active”无效 - Property 'spring.profiles.active' imported from location 'class path resource [application-dev.yml]' is invalid 春季启动:spring.profiles.active = dev / test / prod - Spring Boot: spring.profiles.active=dev/test/prod
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM