我需要根据不同的当前环境配置文件编写不同的逻辑.如何从Spring获取当前的活动和默认配置文件?
我有两个Spring配置文件:dev
和test
.我想在服务器环境中设置活动配置文件,我不想在我的代码中设置它,这样无论我在何处部署应用程序,都会根据服务器中的配置文件加载配置文件.我怎样才能做到这一点?
我有一个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
作为活动配置文件.
在我的应用程序中,我使用了几个配置文件来使某些bean符合自动装配的条件.我缺少的是当某个配置文件不活动时,可以使豆符合自动装配的条件.
我想到的最好的方法是这样的:
X
为配置文件激活not_X
.在我的情况下,如果我想要一个bean在配置文件B不活动时有资格进行自动装配,我会注释它们@Profile("not_B")
然而,该解决方案需要关于所有可能的配置文件的前期知识.
你能想到更好的解决方案吗?
我有一个使用另一个项目的Spring项目.每个项目都有自己的弹簧配置文件,使用applicationContext.xml
和*.properties
为每个配置文件初始化java代码.我从中注入了个人资料args[]
.问题是第二个项目使用env的默认配置, applicationContext.xml
我不能将env注入args[]
第二个项目,我试着找一篇文章来解释Spring profile的工作原理.
applicationContext.xml
?applicationContext.xml
配置更强大?关于该主题甚至实例的文章将非常感谢!! 提前致谢.
现在我有使用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开始了
Spring Profile注释允许您选择配置文件.但是,如果您阅读文档,它只允许您使用OR操作选择多个配置文件.如果指定@Profile("A","B"),那么如果配置文件A或配置文件B处于活动状态,则bean将启动.
我们的用例不同,我们希望支持多种配置的TEST和PROD版本.因此,有时我们只想在配置文件TEST和CONFIG1都处于活动状态时自动装配bean.
有没有办法用Spring做到这一点?什么是最简单的方法?
作为开发人员,我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
财产似乎不像以前那样工作。
请告诉我如何配置我的配置文件,以便获得与迁移前相同的结果。(我不想在这里使用配置文件组)
提前致谢!
我使用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活动配置文件时优先顺序是什么.
假设我希望default
配置文件处于活动状态,但是dev
当有几个相同的元素(例如bean)可供选择但具有不同的配置文件时,配置文件会覆盖它...
比方说,我有两个PropertySourcesPlaceholderConfigurer
bean配置"default"
和"dev"
值环境配置文件.
如果我使用以下配置文件激活: -Dspring.profiles.active="default,dev"
将dev
配置文件覆盖default
吗?
如果不是,如何实现上述行为?
spring ×10
spring-profiles ×10
java ×7
spring-boot ×4
maven ×2
flyway ×1
negation ×1
profile ×1
spring-mvc ×1