Java 在 Spring-boot 中设置默认的活动配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37700352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:35:04  来源:igfitidea点击:

Setting the default active profile in Spring-boot

javaspringspring-boot

提问by DarVar

I want my default active profile to be productionif -Dspring.profiles.activeis not set.

我希望我的默认活动配置文件为productionif-Dspring.profiles.active未设置。

I tried the following in my application.propertiesbut it did't work:

我在我的尝试了以下application.properties但没有奏效:

spring.profiles.default=production

Spring-boot version = 1.3.5.RELEASE

Spring-boot 版本 = 1.3.5.RELEASE

采纳答案by PaulNUK

What you are doing here is setting the default defaultprofile (the profile that is used on any bean if you don't specify the @Profileannotation) to be production.

您在这里所做的是将默认的默认配置文件(如果您未指定@Profile注释,则在任何 bean 上使用的配置文件)设置为production.

What you actually need to do is set the default activeprofile, which is done like this:

你真正需要做的是设置默认的活动配置文件,它是这样完成的:

spring.profiles.active=production

回答by alxpez

If you're using maven I would do something like this:

如果你使用 maven,我会做这样的事情:

Being productionyour default profile:

作为生产您的默认配置文件:

<properties>
    <activeProfile>production</activeProfile>
</properties>

And as an example of other profiles:

作为其他配置文件的示例:

<profiles>
    <!--Your default profile... selected if none specified-->
    <profile>
        <id>production</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <activeProfile>production</activeProfile>
        </properties>
    </profile>

    <!--Profile 2-->
    <profile>
        <id>development</id>
        <properties>
            <activeProfile>development</activeProfile>
        </properties>
    </profile>

    <!--Profile 3-->
    <profile>
        <id>otherprofile</id>
        <properties>
            <activeProfile>otherprofile</activeProfile>
        </properties>
    </profile>
<profiles>

In your application.properties you'll have to set:

在您的 application.properties 中,您必须设置:

spring.profiles.active=@activeProfile@

This works for me every time, hope it solves your problem.

这每次都对我有用,希望它能解决您的问题。

回答by Tom Van Rossom

Put this in the App.java:

把它放在 App.java 中:

public static void main(String[] args) throws UnknownHostException {
    SpringApplication app = new SpringApplication(App.class);
    SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
    if (!source.containsProperty("spring.profiles.active") &&
            !System.getenv().containsKey("SPRING_PROFILES_ACTIVE")) {

        app.setAdditionalProfiles("production");
    }
    ...
}

This is how it is done in JHipster

这就是在 JHipster 中的做法

回答by scorpp

I do it this way

我这样做

    System.setProperty("spring.profiles.default", "dev");

in the very beginning of main(...)

在最开始的时候 main(...)

回答by Jaya Naresh

add --spring.profiles.active=production

添加 --spring.profiles.active=production

Example:

例子:

java -jar file.jar --spring.profiles.active=production

回答by user1653042

Currently using Maven + Spring Boot. Our solution was the following:

目前使用 Maven + Spring Boot。我们的解决方案如下:

application.properties

应用程序属性

#spring.profiles.active= # comment_out or remove

securityConfig.java

安全配置文件

@Value(${spring.profiles.active:[default_profile_name]}")
private String ACTIVE_PROFILE_NAME;

Credit starts with MartinMlima. Similar answer provided here:

信用从MartinMlima开始。此处提供了类似的答案:

How do you get current active/default Environment profile programmatically in Spring?

如何在 Spring 中以编程方式获取当前的活动/默认环境配置文件?

回答by Rafael S. Fijalkowski

First of all, with the solution below, is necessary to understand that always the spring boot will read the application.propertiesfile. So the other's profile files only will complement and replacethe properties defined before.

首先,通过下面的解决方案,有必要了解spring boot总是会读取application.properties文件。所以对方的配置文件只会补充和替换之前定义的属性。

Considering the follow files:

考虑以下文件:

application.properties
application-qa.properties
application-prod.properties

1) Very important. The application.properties, and just this file, must have the follow line:

1)非常重要。的application.properties,而眼前这个文件,必须有后续行:

[email protected]@

2) Change what you want in the QA and PROD configuration files to see the difference between the environments.

2) 在 QA 和 PROD 配置文件中更改您想要的内容以查看环境之间的差异。

3) By command line, start the spring boot app with any of this options:

3) 通过命令行,使用以下任一选项启动 spring boot 应用程序:

It will start the app with the default application.propertiesfile:

它将使用默认application.properties文件启动应用程序:

mvn spring-boot:run

It will load the default application.propertiesfile and after the application-qa.propertiesfile, replacing and/or complementing the default configuration:

它将加载默认application.properties文件,并在application-qa.properties文件之后,替换和/或补充默认配置:

mvn spring-boot:run -Dspring.profiles.active=qa

The same here but with the production environment instead of QA:

此处相同,但使用生产环境而不是 QA:

mvn spring-boot:run -Dspring.profiles.active=prod

回答by Jay

In AWS LAMBDA:

在 AWS LAMBDA 中:

For $ sam localyou add the following line in your sam template yml file:

对于$ sam local您在 sam 模板 yml 文件中添加以下行:

Resources:
   FunctionName:
       Properties:
           Environment:
               Variables:
                  SPRING_PROFILES_ACTIVE: local

But in AWS Console: in your Lambda Environment variables just add:

但是在 AWS 控制台中:在您的 Lambda 环境变量中添加:

KEY:JAVA_TOOL_OPTIONSVALUE:-Dspring.profiles.active=dev

关键:JAVA_TOOL_OPTIONS价值:-Dspring.profiles.active=dev

enter image description here

在此处输入图片说明

回答by Santhoshm

One can have separate application properties files according to the environment, if Spring Boot application is being created. For example - properties file for dev environment, application-dev.properties:

如果正在创建 Spring Boot 应用程序,则可以根据环境拥有单独的应用程序属性文件。例如 - 开发环境的属性文件 application-dev.properties

spring.hivedatasource.url=<hive dev data source url>
spring.hivedatasource.username=dev
spring.hivedatasource.password=dev
spring.hivedatasource.driver-class-name=org.apache.hive.jdbc.HiveDriver

application-test.properties:

应用程序-test.properties:

spring.hivedatasource.url=<hive dev data source url>
spring.hivedatasource.username=test
spring.hivedatasource.password=test
spring.hivedatasource.driver-class-name=org.apache.hive.jdbc.HiveDriver

And a primary application.properties file to select the profile:

还有一个主要的 application.properties 文件来选择配置文件:

application.properties:

应用程序属性:

spring.profiles.active=dev
server.tomcat.max-threads = 10
spring.application.name=sampleApp

Define the DB Configuration as below:

定义数据库配置如下:

@Configuration
@ConfigurationProperties(prefix="spring.hivedatasource")
public class DBConfig {

    @Profile("dev")
    @Qualifier("hivedatasource")
    @Primary
    @Bean
    public DataSource devHiveDataSource() {
        System.out.println("DataSource bean created for Dev");
        return new BasicDataSource();
    }

    @Profile("test")
    @Qualifier("hivedatasource")
    @Primary
    @Bean
    public DataSource testHiveDataSource() {
        System.out.println("DataSource bean created for Test");
        return new BasicDataSource();
    }

This will automatically create the BasicDataSource bean according to the active profile set in application.properties file. Run the Spring-boot application and test.

这将根据 application.properties 文件中设置的活动配置文件自动创建 BasicDataSource bean。运行 Spring-boot 应用程序并进行测试。

Note that this will create an empty bean initially until getConnection() is called. Once the connection is available you can get the url, driver-class, etc. using that DataSource bean.

请注意,这将最初创建一个空 bean,直到调用 getConnection()。一旦连接可用,您就可以使用该 DataSource bean 获取 url、驱动程序类等。

回答by mchawre

We to faced similar issue while setting spring.profiles.activein java.

我们spring.profiles.active在java中设置时遇到了类似的问题。

This is what we figured out in the end, after trying four different ways of providing spring.profiles.active.

这是我们在尝试了四种不同的提供spring.profiles.active.

In java-8

java-8

$ java --spring.profiles.active=dev -jar my-service.jar
Gives unrecognized --spring.profiles.active option.
$ java -jar my-service.jar --spring.profiles.active=dev
# This works fine
$ java -Dspring.profiles.active=dev -jar my-service.jar
# This works fine
$ java -jar my-service.jar -Dspring.profiles.active=dev
# This doesn't works

In java-11

java-11

$ java --spring.profiles.active=dev -jar my-service.jar
Gives unrecognized --spring.profiles.active option.
$ java -jar my-service.jar --spring.profiles.active=dev
# This doesn't works
$ java -Dspring.profiles.active=dev -jar my-service.jar
# This works fine
$ java -jar my-service.jar -Dspring.profiles.active=dev
# This doesn't works

NOTE:If you're specifying spring.profiles.activein your application.propertiesfile then make sure you provide spring.config.locationor spring.config.additional-locationoption to java accordingly as mentioned above.

注意:如果您spring.profiles.activeapplication.properties文件中指定,请确保如上所述相应地提供spring.config.locationspring.config.additional-location选择 java。