标签: spring-profiles

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

我需要根据不同的当前环境配置文件编写不同的逻辑.如何从Spring获取当前的活动和默认配置文件?

java spring spring-profiles

128
推荐指数
9
解决办法
11万
查看次数

设置Spring Profile变量

我有两个Spring配置文件:devtest.我想在服务器环境中设置活动配置文件,我不想在我的代码中设置它,这样无论我在何处部署应用程序,都会根据服务器中的配置文件加载配置文件.我怎样才能做到这一点?

spring spring-profiles spring-boot

60
推荐指数
4
解决办法
9万
查看次数

如何使用maven配置文件设置弹簧活动配置文件

我有一个maven作为构建工具的应用程序.

我正在使用maven配置文件来设置不同配置文件的不同属性.

我想要做的是maven中的所有活动配置文件也将被移植到spring active配置文件中,所以我可以在bean signature(@profile)中引用它们.但我不知道该怎么做.

例如:考虑以下maven设置

<profiles>
    <profile>
        <id>profile1</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
        </properties>
    </profile>
    <profile>
        <id>profile2</id>
        <properties>
        </properties>
    </profile>
    <profile>
        <id>development</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
        </properties>
    </profile>
    <profile>
        <id>production</id>
        <properties>    
        </properties>
    </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

假设我运行maven而没有指定我希望Spring拥有的任何其他配置文件profile1以及 development作为活动配置文件.

java spring maven spring-profiles

56
推荐指数
4
解决办法
7万
查看次数

在配置文件处于非活动状态时包含bean定义

在我的应用程序中,我使用了几个配置文件来使某些bean符合自动装配的条件.我缺少的是当某个配置文件不活动时,可以使豆符合自动装配的条件.

我想到的最好的方法是这样的:

  1. 假设我们有一个所有可能的配置文件列表,例如{A,B,C,D}.
  2. 对特定执行有效的配置文件是{A,C}.
  3. 我所做的是为所有可能无效的配置文件创建人工配置文件.在示例中,我将创建{not_B,not_D}个人资料.
  4. 我希望基于非活动配置文件激活的bean我X为配置文件激活not_X.在我的情况下,如果我想要一个bean在配置文件B不活动时有资格进行自动装配,我会注释它们@Profile("not_B")

然而,该解决方案需要关于所有可能的配置文件的前期知识.

你能想到更好的解决方案吗?

java spring negation spring-profiles

53
推荐指数
1
解决办法
3万
查看次数

如何从系统变量设置Spring配置文件?

我有一个使用另一个项目的Spring项目.每个项目都有自己的弹簧配置文件,使用applicationContext.xml*.properties为每个配置文件初始化java代码.我从中注入了个人资料args[].问题是第二个项目使用env的默认配置, applicationContext.xml 我不能将env注入args[]第二个项目,我试着找一篇文章来解释Spring profile的工作原理.

  1. 是否存在一个层次结构,当未配置默认值时,它将查看配置文件applicationContext.xml
  2. System var是否比applicationContext.xml配置更强大?
  3. 您认为什么是我挑战的最佳解决方案?

关于该主题甚至实例的文章将非常感谢!! 提前致谢.

java spring spring-profiles

45
推荐指数
5
解决办法
10万
查看次数

如何在某些弹簧剖面中禁用飞路?

现在我有使用ms sql server的spring-boot应用程序.我们使用flyway fr迁移.

我想为测试添加额外的配置文件.我想从实体类生成表.并且不要使用飞路.

我试过smth在application.yaml中这样写

spring:
  profiles: test
  jpa:
      generate-ddl: true
      hibernate:
  datasource:
    url: jdbc:h2:mem:test_db;MODE=MSSQLServer
    username: sa
    password:
Run Code Online (Sandbox Code Playgroud)

但无论如何,flyway开始了

java spring flyway spring-profiles spring-boot

43
推荐指数
3
解决办法
2万
查看次数

Spring:如何在Profiles中做AND?

Spring Profile注释允许您选择配置文件.但是,如果您阅读文档,它只允许您使用OR操作选择多个配置文件.如果指定@Profile("A","B"),那么如果配置文件A或配置文件B处于活动状态,则bean将启动.

我们的用例不同,我们希望支持多种配置的TEST和PROD版本.因此,有时我们只想在配置文件TEST和CONFIG1都处于活动状态时自动装配bean.

有没有办法用Spring做到这一点?什么是最简单的方法?

java spring spring-profiles

33
推荐指数
7
解决办法
2万
查看次数

在 spring boot 2.4.0 版本中包含配置文件

作为开发人员,我dev在本地开发环境中使用默认配置文件。这是我的application-dev.properties文件的一部分:

# Profiles    
spring.profiles.include=auth
Run Code Online (Sandbox Code Playgroud)

以前我在运行时使用 Spring Boot 2.3.0.RELEASE 和spring.profiles.include属性包含auth配置文件。

但是在我迁移到 Spring Boot 2.4.0 之后,我没有auth启用配置文件。spring.profiles.include财产似乎不像以前那样工作。

请告诉我如何配置我的配置文件,以便获得与迁移前相同的结果。(我不想在这里使用配置文件组)

提前致谢!

java profile spring spring-profiles spring-boot

26
推荐指数
3
解决办法
2万
查看次数

SpringBoot:无法从以下候选者中找到单个主类

我使用Spring Initializer,嵌入式Tomcat,Thymeleaf模板引擎生成了Spring Boot Web应用程序.使用的技术:Spring Boot 1.4.2.RELEASE,Spring 4.3.4.RELEASE,Thymeleaf 2.1.5.RELEASE,Tomcat Embed 8.5.6 ,Maven 3,Java 8

我有一个SpringBoot应用程序.有这两个类:

@Profile("!war")
@SpringBootApplication
@Import({SecurityConfig.class ,PersistenceConfig.class, ServiceConfig.class})
public class BookApplication {

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

@Profile("war")
@Import({SecurityConfig.class ,PersistenceConfig.class})
@SpringBootApplication
public class BookApplicationWar extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(BookApplicationWar.class);
    }

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

}
Run Code Online (Sandbox Code Playgroud)

我用这个命令生成战争

 mvn clean package -DskipTests -Dspring.profiles.active=pebloc,war -DAPP-KEY=pebloc
Run Code Online (Sandbox Code Playgroud)

但我得到了这个错误:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) on …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc maven spring-profiles spring-boot

23
推荐指数
7
解决办法
3万
查看次数

当spring.profiles.active设置了多个Spring的环境配置文件时,优先顺序是什么

我只是想知道在指定多个Spring活动配置文件时优先顺序是什么.

假设我希望default配置文件处于活动状态,但是dev当有几个相同的元素(例如bean)可供选择但具有不同的配置文件时,配置文件会覆盖它...

比方说,我有两个PropertySourcesPlaceholderConfigurerbean配置"default""dev"值环境配置文件.

如果我使用以下配置文件激活: -Dspring.profiles.active="default,dev"

dev配置文件覆盖default吗?

如果不是,如何实现上述行为?

spring spring-profiles

21
推荐指数
4
解决办法
2万
查看次数