问题标签 [spring-profiles]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
1250 浏览

java - 如何切换 contextLoadListener 取决于弹簧配置文件

我需要在应用程序启动之前执行代码。

我写了以下监听器(在 web.xml 中注册):

我希望只有在test配置文件被激活时才会调用监听器。
但它总是调用。如何解决这种行为?

0 投票
1 回答
597 浏览

java - 在 WebApplicationInitializer 中获取实际的弹簧配置文件

我在 web.xml 中有以下条目:

并遵循 WebInitializer:

当我使用测试配置文件(mvn clean install jetty:run-Dspring.profiles.active=test)启动应用程序时 - 执行进入 if 语句,我在控制台中看到结果。

如果我在没有显式配置文件 ( mvn clean install jetty:run) 的情况下启动应用程序 - 执行不会进入 if 语句。从我这边看是错误的。PS我试图执行rootContext.getEnvironment().getDefaultProfiles()但它返回带有值的单元素数组default(应该test根据我的意见)

如何解决这个问题?

0 投票
0 回答
365 浏览

java - 使用 java config 初始化 spring 属性(配置文件等)的正确方法是什么?

我有一个名为 initParameters.properties 的文件,其中包含一个名为 spring.profiles.active 的键,它正在被过滤,但这还不够,因为 Spring 还不知道这个属性。
我使用AbstractAnnotationConfigDispatcherServletInitializer作为我的 WebAppInitializer
,这是它的重要部分:

选项1:

选项 2:

两种方式都有效,我只是不知道它们之间有什么区别,除了一个是系统属性,另一个是 initParameter。
Ofc,当我在启动应用程序时设置系统属性 -Dspring.profiles.active=someProfile 时,它​​会覆盖所有内容,甚至是我的 .properties 文件中的过滤,所以一切都很好。
我在整个互联网上寻找一些解释,优点/缺点,但找不到任何东西。
此外,如果有一种方法可以直接从 .properties 文件通知 spring 属性,我也想知道如何(也就是跳过 setInitParameters 部分)。我认为 Spring Boot 会自动检测到它,但我没有使用它,所以想知道 Spring 中是否有类似的东西。谢谢你的时间。

0 投票
1 回答
3784 浏览

java - 在 junit 测试中禁用 RedisHttpSessionConfiguration

我正在使用spring-bootspring-session构建一个简单的 rest api。生产应用程序应使用 redis-session 实现,而测试应使用默认(非 redis)spring-session。

但是,当spring-boot-starter-redis在项目中声明 maven 依赖项时,spring-boot 将自动创建一个org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration,而@EnableRedisHttpSession注释不在测试配置中的任何位置。

我的设置

申请代码:

测试:

如何在 junit 测试中禁用 redis 会话自动配置?

测试期间的堆栈跟踪:

编辑

我想到了。RedisHttpSessionConfig尽管配置上没有注释,但在测试中加载的原因是EnableRedisSessionspring-boot-starter-web 附带的自动配置:SessionAutoConfiguration

所以在测试中,这是需要从自动配置中排除的类:

0 投票
1 回答
215 浏览

spring-boot - Spring boot, use profiles to load files

I have used spring-boot profiles to change property values for different environments, now I want to use the same approach to load different resource files, ie example I have dev_queries and prod_queries.xml with sql queries.

How can I make spring-boot load dev_queries.xml if active profile is dev and prod_queries.xml otherwise. I know that I can check the active profile but my idea is to do not add specific logic for handle this situation.

0 投票
4 回答
19048 浏览

java - Spring Boot Configuration 在多个@Profile 上跳过注册

我有一个具有不同配置文件设置的 Spring Boot 应用程序:dev、、、prod等。qcconsole

两个配置类的设置如下。MyConfigurationA应该为所有配置文件注册,除了console. MyConfigurationB应该注册除了consoledev

当我使用 profile 运行应用程序时consoleMyConfigurationA没有注册 - 这很好。但是MyConfigurationB被注册了——我不想要。我已@Profile按如下方式设置注释,以不注册MyConfigurationBfor profile consoledev.

但是MyConfigurationB当我使用 profile 运行应用程序时,它正在注册console


文档(http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Profile.html)有一个包含一个配置文件并排除另一个配置文件的示例。在我的示例中,我将两者都排除在外@Profile({"!p1", "!p2"}):

@Profile({"p1", "!p2"}),如果配置文件 'p1' 处于活动状态或配置文件 'p2' 不活动,则将发生注册。

我的问题是:我们如何跳过注册两个配置文件的配置?@Profile({"!p1", "!p2"})正在做 OR 操作。我们在这里需要 AND 操作。


编码 :

0 投票
1 回答
3081 浏览

java - 是否可以配置 cucumber 以使用不同的弹簧配置文件运行相同的测试?

我有一个应用程序,我正在使用不同的技术进行试验。我有一组用每种技术实现的接口,我使用 spring 配置文件来决定运行哪种技术。每种技术都有自己的 Spring java 配置,并用它们活跃的配置文件进行注释。

我运行我的黄瓜测试来定义哪个配置文件是活动的,但这迫使我每次想要测试不同的配置文件时手动更改字符串,从而无法为所有这些配置文件运行自动化测试。黄瓜中是否有提供一组配置文件以便为每个配置文件运行一次测试?

谢谢!

0 投票
1 回答
912 浏览

java - JUnit + Spring 配置文件:无法加载 JDBC 驱动程序类

如何修复下一个异常?

在配置中看不到任何问题。

它运行良好,但经过一些重构失败。

例外

配置

JUnit

来自 pom.xml

配置文件.xml

来自 data-source.xml

属性/测试数据库.properties

0 投票
2 回答
14735 浏览

java - Maven 和 spring-boot:如何在 spring-boot:repackage 上指定配置文件

使用 spring-boot,我知道我可以拥有配置文件并根据活动配置文件使用不同的配置文件。例如命令:

“mvn spring-boot:run -Drun.profiles=default,production”

将使用“application-default.properties”和“application-production.properties”中定义的设置运行我的spring-boot应用程序,第二个文件上的设置覆盖第一个文件中定义的相同设置(例如db连接设置)。目前这一切都运行良好。

但是,我想构建我的 spring-boot 应用程序并使用以下命令生成一个可运行的 jar:

“mvn 包 spring-boot:repackage”。

该命令确实可以很好地生成独立的可运行 jar。问题是,¿如何使用前一个命令指定活动配置文件?我用过

“mvn package spring-boot:repackage -Drun.profiles=default,production”

但它不工作。

0 投票
2 回答
360 浏览

java - Spring profiles: Which element should be parsed?

I have the following configuration in my spring context:

There is question which <beans> elements will be parsed take into account: that prof1 and prof2 profiles are activated.

It looks like it always choose this one <beans profile="prof1" >, but not sure why it does not choose another one <beans profile="!prof1" >. Can I relay that it always will choose <beans> without exclamation mark?