标签: spring-profiles

可以通过属性文件设置Spring活动配置文件吗?

我希望能够从属性文件中读取活动配置文件,以便可以在基于Spring MVC的Web应用程序中使用不同的配置文件配置不同的环境(dev,prod等).我知道可以通过JVM参数或系统属性设置活动配置文件.但我想通过属性文件来代替.关键是我不静态地知道活动配置文件,而是想从属性文件中读取它.看起来这是不可能的.例如,如果我在application.properties中有'spring.profiles.active = dev',并允许在override.properties中覆盖它,如下所示:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:/application.properties</value>
            <value>file:/overrides.properties</value>
        </list>
    </property>
</bean> 
Run Code Online (Sandbox Code Playgroud)

该配置文件未在环境中被拾取.我想这是因为在bean初始化之前正在检查活动的配置文件,因此不尊重在属性文件中设置的属性.我看到的唯一另一个选项是实现一个ApplicationContextInitializer,它将按优先级顺序加载这些属性文件(如果存在则覆盖首先是override.properties,否则是application.properties)并在context.getEnvironment()中设置值.有没有更好的方法从属性文件中执行此操作?

spring spring-mvc spring-profiles spring-properties

6
推荐指数
1
解决办法
1915
查看次数

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 connection设置)。所有这些目前运行良好。

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

“mvn 包 spring-boot:repackage”。

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

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

但它不起作用。

java spring maven spring-profiles spring-boot

6
推荐指数
2
解决办法
1万
查看次数

如何从环境变量添加活动弹簧轮廓?

到现在为止,我在我的设置中设置了以下环境变量~/.bash_profile:

export SPRING_PROFILES_ACTIVE=local
Run Code Online (Sandbox Code Playgroud)

这设定了我的活跃弹簧轮廓.但是现在,我想本地配置文件添加到其中定义的其他配置文件中,application.properties而不是替换它们.

Spring Boot文档中,有一节关于添加活动配置文件,但我没有看到从环境变量添加活动配置文件.

我试图设置SPRING_PROFILES_INCLUDE环境变量,但这没有效果.

这该怎么做?

PS:我正在使用Spring Boot 1.4.2.

spring spring-profiles spring-boot

6
推荐指数
2
解决办法
1万
查看次数

@IfProfileValue不能使用JUnit 5 SpringExtension

我使用junit5和spring-starter-test,为了运行spring test我需要使用@ExtendWith而不是@RunWith.但是,@IfProfileValue使用@RunWith(SpringRunner.class)但不使用@ExtendWith(SpringExtension.class),下面是我的代码:

@SpringBootTest
@ExtendWith({SpringExtension.class})
class MyApplicationTests{

    @Test
    @DisplayName("Application Context should be loaded")
    @IfProfileValue(name = "test-groups" , value="unit-test")
    void contextLoads() {
    }
}
Run Code Online (Sandbox Code Playgroud)

所以contextLoads应该被忽略,因为它没有指定env test-grooups.但测试只是运行而忽略了@IfProfileValue.

junit spring-test spring-profiles spring-boot junit5

6
推荐指数
1
解决办法
759
查看次数

Gradle构建spring boot应用程序作为战争与活动配置文件

我想将我的spring boot应用程序打包为特定配置文件的战争.这可以通过在application.properties文件中设置spring.profiles.active = profile name来完成.是否有可能在建立战争时将其设置为参数,例如.gradle build --spring.profiles.active = 个人资料名称

gradle spring-profiles spring-boot

5
推荐指数
1
解决办法
4205
查看次数

Spring YAML配置文件配置

我不确定我是否很好地了解Spring配置文件如何与yaml和属性文件一起使用。我试图混杂这两种类型的配置(这两个文件不共享任何配置),但是从yaml配置读取配置文件时遇到了问题。

我正在使用Spring 4.1.1

这是代码。这是context:property-placeholder配置:

<context:property-placeholder location="classpath:/job-config.properties" order="1" 
ignore-unresolvable="true" ignore-resource-not-found="false"/>


<context:property-placeholder properties-ref="yamlProperties" order="2"
ignore-resource-not-found="false" ignore-unresolvable="true"/>
Run Code Online (Sandbox Code Playgroud)

其中yamlProperties是以下bean

    <bean id="yamlProperties" 
    class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
                <property name="resources" 
value="file:${catalina.home}/properties/test.yml"/>
            </bean>
Run Code Online (Sandbox Code Playgroud)

这是test.yml

spring:
  profiles.default: default
---
spring:
  profiles: default
db:
  url: jdbc:oracle:thin:@##hostname##:##port##:##SID##
  usr: ##USER##
  pwd: ##PWD##
---
spring:
  profiles: development
db:
  url: jdbc:oracle:thin:@##hostname##:##port##:##SID_DEVELOPMENT##
  usr: ##USER_DEVELOPMENT##
  pwd: ##PWD_DEVELOPMENT##
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我尝试通过以下方式配置(通过xml)我的数据源时:

<property name="url" value="${db.url}"/>
<property name="username" value="${db.usr}"/>
<property name="password" value="${db.pwd}"/>
Run Code Online (Sandbox Code Playgroud)

Spring始终使用YAML文件中的最后一个配置,而忽略该配置文件。我试图通过web.xml中的contex-parameter传递活动配置文件,或者直接将其传递给JVM(我实现了一个实现EnvironmentAware接口的bean,以获取活动/默认配置文件,并且是正确的),但看起来一切都很好,但是,当尝试注入值将忽略配置文件。

我相信使用property-placeholder上下文(带有订单)可以获得一个Property-placeholder,它是PropertySourcesPlaceholderConfigurer的一个实例,因此可以访问Environment,但是我无法理解为什么配置文件被忽略并且spring获得了最后的yaml文件配置。

我在63.6节http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html中添加了对文档的引用(spring-boot)。

提前致谢

spring yaml spring-profiles spring-properties property-placeholder

5
推荐指数
1
解决办法
3292
查看次数

如何以编程方式为任何环境设置活动配置文件?

我想为任何环境设置依赖于活动配置文件的主机,但找不到与环境无关的钩子。

以下工厂将在构建应用程序上下文之前设置活动配置文件。

/META-INF/spring.factories

org.springframework.context.ApplicationContextInitializer=MyApplicationContextInitializer
Run Code Online (Sandbox Code Playgroud)

MyApplicationContextInitializer.java

public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext ca) {
        ConfigurableEnvironment environment = ca.getEnvironment();
        environment.addActiveProfile("myHost");
    }
}
Run Code Online (Sandbox Code Playgroud)

如果此示例由 JUnit 在模拟环境中执行...

*测试.java

...
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
...
Run Code Online (Sandbox Code Playgroud)

... 以下将被记录。

...
... RestControllerTests   : The following profiles are active: myHost
...
Run Code Online (Sandbox Code Playgroud)

但配置文件myHost未激活,默认配置文件将在 JUnit 的上下文中使用!

带有 VM 参数的 Java 应用程序和 JUnit 测试有效......

-Dspring.profiles.active=myHost
Run Code Online (Sandbox Code Playgroud)

我使用 war 打包的 spring-boot-starter-web 应用程序,并以编程方式设置配置文件并在任何环境中使用

  • Java应用程序
  • JUnit
  • Servlet 容器

如何以编程方式为任何环境设置配置文件?

我不想使用 VM 参数或环境变量,因为配置文件应由当前主机名设置。

spring spring-mvc spring-profiles spring-boot spring-boot-test

5
推荐指数
2
解决办法
1万
查看次数

在 application.properties 中使用 spring.profile.default 时,Spring Boot 不加载默认配置文件

仅供参考:我发誓没有激活配置文件配置,例如 -D 或运行配置

\n\n

目标

\n\n

当应用程序在没有任何激活配置文件的情况下启动时,dev配置文件将默认激活。

\n\n

问题

\n\n

我已经设置了spring.profile.default = dev,并且我希望开发配置文件被激活。但事实并非如此。

\n\n

我做了什么

\n\n

Run Environment

\n\n

Spring-Boot-版本:2.1.2 发布

\n\n

What I\'m referred

\n\n

1) 如何在 Spring Boot 应用程序中使用配置文件 - \n https://www.javacodegeeks.com/2019/07/profiles-spring-boot-application.html#respond

\n\n

这是我所做的代码

\n\n

/resources/application.properties

\n\n
spring.profiles.default= dev\n\napplication.environment=This is a "Default" Environment\n
Run Code Online (Sandbox Code Playgroud)\n\n

/resources/application-dev.properties

\n\n
application.environment=This is a "dev" Environment\nserver.port= 8082\n
Run Code Online (Sandbox Code Playgroud)\n\n

/ProfileController.java

\n\n
@RestController\n@RequestMapping("/v1")\npublic class ProfileController {\n\n    @Value("${application.environment}")\n    private String applicationEnv;\n\n    @GetMapping\n    public String getApplicationEnv(){\n        return applicationEnv;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

结果

\n\n

localhost/v1 …

java spring-profiles spring-boot

5
推荐指数
1
解决办法
2万
查看次数

如何使用 Spring 配置文件设置 Flyway 迁移文件位置

我有两个 Spring 配置文件,devtest针对开发和测试环境进行了配置。在每个环境中,我h2在开发和postgresql测试中使用不同的数据库。以下是我的每个配置文件的属性文件,其中{vendor}由 Spring Boot 解析h2并分别postgresql根据配置的数据源进行解析。

应用程序-dev.properties

spring.flyway.locations=classpath:db/migration/{vendor}
Run Code Online (Sandbox Code Playgroud)

application-test.properties

#Data source
spring.datasource.url=jdbc:postgresql://localhost:5432/test
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

#Flyway
spring.flyway.check-location=false
spring.flyway.locations=classpath:/db/migration/test/{vendor}
Run Code Online (Sandbox Code Playgroud)

dev配置文件的 Flyway 迁移文件位于下test/resourcestest配置文件位于下main/resources

在此处输入图片说明

在此处输入图片说明

当我使用test配置文件运行我的应用程序时它工作正常,它仅在main/resources. 但是,当我使用dev配置文件运行我的单元测试时。我希望它只在src/test/resources/db/migration/h2. 但是 Flyway 正在从中获取迁移文件main/resources并且test/resources两者都导致错误

org.flywaydb.core.api.FlywayException:发现多个版本 1 的迁移

我不明白这种行为。有关如何解决此问题的任何输入?

spring database-migration flyway spring-profiles spring-boot

5
推荐指数
2
解决办法
5300
查看次数

spring中如何根据profile调用bean的不同实现

我需要添加一个接口的多个实现,并且应该根据配置文件选择其中之一。

例如

interface Test{
    public void test();
}

@Service
@Profile("local")
class Service1 implements Test{
    public void test(){

    }
}

@Service
class Service2 implements Test{
    public void test(){

    }
}


@SpringBootApplication
public class Application {

    private final Test test;

    public Application(final Test test) {
        this.test = test;
    }

    @PostConstruct
    public void setup() {
        test.test();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的意图是,当我使用 -Dspring.profiles.active=local 时,应该调用 Service1,否则应该调用 service2,但我收到一个异常,即测试的 bean 丢失。

java spring spring-profiles spring-boot

4
推荐指数
1
解决办法
4290
查看次数