标签: spring-profiles

独家的Spring概要分析bean和DependsOn批注

适用于我的代码:
数据库配置的一部分如下所示:

@Profile("!dbClean")
@Bean(initMethod = "migrate")
public Flyway flywayNotADestroyer() {
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource());
    flyway.setInitOnMigrate(true);
    return flyway;
}

@Profile("dbClean")
@Bean(initMethod = "migrate")
public Flyway flywayTheDestroyer() {
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource());
    flyway.setInitOnMigrate(true);
    flyway.clean();
    return flyway;
}
Run Code Online (Sandbox Code Playgroud)

该配置代表两个互斥豆。存在“ dbClean”概要文件时创建一个,不存在该概要文件时创建另一个。忍受我,忘记代码重复。

另一个配置控制Quartz配置:

@Autowired
private Flyway flyway;

@Bean
public SchedulerFactoryBean quartzScheduler(Flyway flyway) throws SchedulerException {
    SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();

    quartzScheduler.setDataSource(dataSource);
    quartzScheduler.setTransactionManager(transactionManager);
    quartzScheduler.setOverwriteExistingJobs(true);
    quartzScheduler.setSchedulerName("mysuperduperthegratest-quartz-scheduler");

    AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
    jobFactory.setApplicationContext(applicationContext);
    quartzScheduler.setJobFactory(jobFactory);
    quartzScheduler.setQuartzProperties(schedulingProperties);

    return quartzScheduler;
}
Run Code Online (Sandbox Code Playgroud)

上面的作品很有魅力。问题是Quartz配置未使用Flyway自动连接的bean。它只需要在Quartz调度程序之前创建此bean。

因此理想的配置是:

数据库部分:

@Profile("!dbClean")
@Bean(name = …
Run Code Online (Sandbox Code Playgroud)

java spring spring-profiles

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

Spring 在方法上的 @Profile 是一个好的实践吗

我有一个公开休息服务的 Spring Boot Web 应用程序。

我问自己如何正确管理过滤器上的配置文件。实际上,我的应用程序有 2 个配置文件:dev 和 prod(你猜它代表什么......)

在产品模式下,我比开发模式下有更多的过滤器需要激活。

我的过滤器配置类如下:

@Configuration
public class FiltersConfig {

    @Bean
    public FilterRegistrationBean filterRegistrationBean(CompositeFilter compositeFilter){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setFilter(compositeFilter);
        return filterRegistrationBean;
    }

    @Bean
    @Profile("dev")
    public CompositeFilter devCompositeFilter(){
        CompositeFilter compositeFilter = new CompositeFilter();
        List<Filter> filtersList = new ArrayList<>();
        //filtersList.add(filter1());
        compositeFilter.setFilters(filtersList);
        return compositeFilter;
    }


    @Bean
    @Profile("prod")
    public CompositeFilter prodCompositeFilter(){
        CompositeFilter compositeFilter = new CompositeFilter();
        List<Filter> filtersList = new ArrayList<>();
        //filtersList.add(filter1());
        compositeFilter.setFilters(filtersList);
        return compositeFilter;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  • 在方法上添加 @Profile 是一个好习惯吗?
  • 有没有一种方法可以强制编译器排除使用与当前设置不同的配置文件注释的类、方法等?(我不希望我的生产 jar/war …

java spring maven spring-profiles spring-boot

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

Profile Inheritage In spring boot

是否可以在 spring boot 中拥有一个配置文件以及另一个继承大多数父值和 bean 的配置文件?

例如,我有两个配置文件 staging 和 staging-task。我希望 staging-task 继承 staging 配置文件的数据库配置,但是我希望它覆盖 jpa 配置。

配置文件继承是否可用于@Configuration bean。

java spring-profiles spring-boot

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

覆盖多个弹簧活动配置文件的属性值

我有一个带有 application.yml 的 Spring Boot 应用程序。

application.yml的内容:

spring:
  profiles:
    active: default,private
integrations:
  ecom:
    api-url: http://localhost:8080/com
Run Code Online (Sandbox Code Playgroud)

application-private.yml 的内容:

integrations:
  ecom:
    api-url: http://testenv:8080/com
Run Code Online (Sandbox Code Playgroud)

根据我的理解,集成:ecom:api-url 正在从 application-private.yml 加载,即使默认配置文件也具有相同的属性。

如果两个配置文件处于活动状态,是否会按照指定配置文件的顺序加载和使用该属性?

我的订单:

-Dspring.profiles.active="default,private"
Run Code Online (Sandbox Code Playgroud)

提前致谢。

spring spring-profiles spring-boot

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

使用Spring配置文件启用/禁用web.xml过滤器

我有一个Spring应用程序,可以使用两个不同的持久性API:

  • Spring Data JPA
  • Spring Data Neo4j

使用Spring Data JPA时,我需要在"web.xml"中声明"OpenEntityManagerInViewFilter"来进行延迟加载:

<filter>
    <filter-name>OpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>OpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)

问题是我在使用Spring Data Neo4j时无法保持此过滤器.保持启用状态会导致以下运行时错误:

No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
Run Code Online (Sandbox Code Playgroud)

我想选择与Spring配置文件一起使用的数据库(例如spring.profiles.active=relational-databasespring.profiles.active=graph-database).

问题:当配置文件是"关系数据库"时,如何启用"OpenEntityManagerInViewFilter",当配置文件是"图形数据库"时,如何禁用它?

谢谢!

相关问题:

spring web.xml spring-profiles

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

将多个 Spring 配置文件应用到单个配置类

我的应用程序有 4 个可能的 Spring 配置文件

  • 测试(这用于单元/集成测试)
  • 质量保证
  • 阶段
  • 生产

上面列表中的最后 3 个是该应用程序将运行的实际环境。

这是我的配置类 AppConfig 的概述

@Configuration
@ComponentScan(basePackages={"org.mycompany.services","org.mycompany.domain", "org.mycompany.utils"})
public class AppContext {

}
Run Code Online (Sandbox Code Playgroud)

这是我的问题:我希望 AppContext 的配置仅应用于 QA、STAGE 和 生产配置文件(但不适用于测试配置文件)。最好的方法是什么?

原因是上面的配置类从 org.mycompany.services 中扫描并选择类。但我不希望为 TEST 配置文件选取此包,因为我打算使用 Mockito 模拟此包中的类。

另一个可能的问题是,我是否需要重新考虑进行单元/集成测试的方式,以便可以简化对 org.mycompany.service 包中的类的模拟?

java testing spring integration-testing spring-profiles

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

Spring Boot会为另一个项目抛出带有maven依赖关系的ClassNotFoundException

我有Spring Boot项目,EnvironmentPostProcessor实现简单:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;

public class DevProfilerResolverEnvironmentPostProcessor implements EnvironmentPostProcessor {

        @Override
        public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
            if (configurableEnvironment.getActiveProfiles().length == 0) {
                if (System.getenv().get("OS").contains("Windows")) {
                    configurableEnvironment.addActiveProfile("DEV");
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

另外,我将这个课程注册到sprig.factories:

org.springframework.boot.env.EnvironmentPostProcessor = com.example.demo.DevProfilerResolverEnvironmentPostProcessor
Run Code Online (Sandbox Code Playgroud)

现在结构看起来像:

在此输入图像描述

来自pom文件的片段:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
Run Code Online (Sandbox Code Playgroud)

我和Maven一起执行:

mvn安装 …

java post-processing maven spring-profiles spring-boot

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

使用Maven配置构建特定于Spring Boot配置文件的属性

如何使用Maven为Spring Boot应用程序构建特定于环境的war文件。我已经在src / main / resource /文件夹中创建了3个配置文件配置文件。

  1. application.prod.properties
  2. application.dev.properties
  3. application.test.properties

我可以在将项目作为Spring Boot应用程序执行时,通过在VM参数选项卡中使用值“ -Dspring.profiles.active = dev”指定所需的配置文件类型来运行应用程序。

在这里,作为spring boot应用程序运行时,我可以指定所需的配置文件。以同样的方式,当我需要使用不同的配置文件进行MAVEN安装时。是否可以在Maven安装目标的“运行配置”目标中将配置文件指定为VM参数列表的一部分。

我的局限性在于不要触摸现有的Java代码。

我正在使用STS IDE,Spring boot 1.5.2.RELEASE版本,Java 1.8和oracle db。

同样,这也有助于我在Jenkins中配置配置文件。

我的配置文件配置有两个要求。

  1. 使用VM参数在STS IDE中作为Spring Boot应用程序运行该应用程序。使用以下VM ARGS -Dspring.profiles.active=dev

块引用

(在开发环境中本地启动SpringBootApp时,这里出现异常)。


申请开始失败


描述:

无法确定数据库类型NONE的嵌入式数据库驱动程序类

行动:

如果您想要嵌入式数据库,请在类路径上放置一个受支持的数据库。如果您要从特定配置文件中加载数据库设置,则可能需要激活它(配置文件“ dev”当前处于活动状态)。

块引用

  1. 如何使用maven install通过动态指定配置文件以生成war文件来执行相同的操作,但找不到解决方案。

java war maven spring-profiles spring-boot

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

Spring 表达式语言 - 如果设置了 Profile,则设置不同的值

我有一堂这样的课:

public AbstractConfig() {
    super(DataConfig.MGR_NAME);
}
Run Code Online (Sandbox Code Playgroud)

里面DataConfig有:

    public final String MGR_NAME = "theManager";
Run Code Online (Sandbox Code Playgroud)

我发现 SpEL 文档令人困惑。如果设置了 Spring 配置文件,有没有办法可以更改值?也就是说,如果我有配置文件“AlternateManager” use theManagerAlt,但默认为theManager其他情况?

虽然我编写了这个符号来获取活动配置文件,但我希望存在一些如下所示的语法来完成这项工作:

    @Value("#PROFILE['AlternateManager'] ? 'theManagerAlt' : 'theManager' ")
    public final String MGR_NAME;
Run Code Online (Sandbox Code Playgroud)

java spring spring-el spring-profiles

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

Spring没有从命令行覆盖活动配置文件

这是我的application.yml文件:

spring:
  freemarker:
    template-loader-path: classpath:/templates

  datasource:
    url: jdbc:postgresql://localhost:5432/myapp
    username: postgres
    password: password
    driver-class-name: org.postgresql.Driver

  jpa:
    show-sql: true
    properties:
      hibernate:
        enable_lazy_load_no_trans: false
        jdbc:
          lob:
            non_contextual_creation: true
        dialect: org.hibernate.dialect.PostgreSQLDialect
    hibernate:
      ddl-auto: create-drop


---

spring:
  profiles:
    active: development


---

spring:
  profiles: staging

  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update

logging:
  level:
    root: DEBUG

---

spring:
  profiles: production

  jpa:
    show-sql: false
    hibernate:
      ddl-auto: update
Run Code Online (Sandbox Code Playgroud)

我使用以下命令运行该应用程序:

java -jar application.jar -Dspring.profiles.active=staging
Run Code Online (Sandbox Code Playgroud)

在日志中,我可以看到spring boot打印出来: 下列配置文件处于活动状态:开发

那么,即使我在命令行args中显式设置了活动配置文件,为什么也不将其配置为暂存

java spring spring-profiles spring-boot

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

如何基于活动配置文件访问application- {profile} .properties文件

我需要在项目位置之外访问application.properties文件。我可以使用以下方法实现相同目的:

@Component
@PropertySources({
        @PropertySource(value = "file:${user.home}/file/path/application.properties", ignoreResourceNotFound = false) })
public class PropConfig implements InitializingBean {

Run Code Online (Sandbox Code Playgroud)

现在,我想使用主动配置文件实现相同的目的。如果开发人员配置文件处于活动状态,则需要获取application-dev.properties,如果阶段配置文件处于活动状态,则需要获取application-stage.properties,依此类推。

我正在将Windows平台和JAVA 8与Spring Boot 1.5.x一起使用

我尝试在application.properties文件中设置活动配置文件。但这不起作用

spring.profiles.active=dev
Run Code Online (Sandbox Code Playgroud)

java spring-profiles spring-boot spring-properties

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

spring.profiles.active 在 springboot 应用程序中不起作用

我在 Java 类中创建了这样的配置文件,

@Profile(value = "cache")
public class MapCache {
....
}
Run Code Online (Sandbox Code Playgroud)

使用spring.profiles.active时,这些配置文件不会被激活,但如果我使用spring.profiles.include配置文件工作正常。

我想通过在 application.properties 中添加的属性来激活配置文件

注意:应用程序在独立的jetty实例中运行。

任何提示都会很棒。

java spring-profiles spring-boot spring-properties

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

spring.profiles.active 不兑现

application.properties 
Run Code Online (Sandbox Code Playgroud)

我们有线路

spring.profiles.active=LOCALHOST
Run Code Online (Sandbox Code Playgroud)

application-DEV.properties
Run Code Online (Sandbox Code Playgroud)

,我们有这条线

spring.profiles.active=DEV,dbcache,metrics,AWS
Run Code Online (Sandbox Code Playgroud)

. 运行应用程序时

java -jar app.war -Dspring.profiles.active=DEV
Run Code Online (Sandbox Code Playgroud)

控制台输出说

The following profiles are active: LOCALHOST
Run Code Online (Sandbox Code Playgroud)

,即

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

参数不被接受,应用程序仍然使用默认的 LOCALHOST 配置文件。

java spring spring-profiles spring-boot spring-properties

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