SpringBoot:配置生产与开发环境 [英] SpringBoot: Configuration Production Vs Development environments
问题描述
我在使用 SpringBoot 1.5.1 时遇到问题.我已经为我的开发环境创建了 application.properties
和 application-dev.properties
.
I have a problem with SpringBoot 1.5.1. I've create application.properties
and application-dev.properties
for my dev enviroment.
主要区别在于持久性:在生产(application.properties
)中有一个 JNDI(在 Tomcat 上配置),而在 dev 中有一个本地数据库(H2).
The main difference is the persistence: in production (application.properties
) there is a JNDI (configured on Tomcat) and in dev there is a local db (H2).
这是我在 application.properties
中的配置:
This is my conf in application.properties
:
spring.datasource.jndi-name=jdbc/db
这是application-dev.properties
:
spring.datasource.url=jdbc:h2:file:~/db
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
但是当我开始使用开发配置文件时
But when I'm starting in with dev profile
2017-02-24 15:25:39.948 INFO 7912 --- [ main] it.geny.MmqApplication : The following profiles are active: dev
我的应用程序停止,因为它没有找到 JNDI jdbc/db!!!!我正在尝试更改我的 application-dev.properties
上的日志配置并且它有效!但不是持久性配置的变化.
my app stops because it didn't find the JNDI jdbc/db!!!! I'm trying to change log configuration on my application-dev.properties
and it works! But not the changes on persistence configuration.
提前致谢
推荐答案
application-dev.properties
的所有属性都会覆盖 application.properties
中的属性.但是,如果 application.properties
中的属性没有在 dev 中设置,它们也会被包含到上下文中.如果启用了属性 spring.datasource.jndi-name
,则所有 spring.datasource
属性都将被忽略.
All the properties of application-dev.properties
overrides the properties in application.properties
. But if in application.properties
are properties which are not set in the dev one they will be also inlcuded to the context. And if the property spring.datasource.jndi-name
is enabled all the spring.datasource
properties are ignored.
解决方案是创建另一个属性文件,如 application-prod.properties
并在那里设置 spring.datasource.jndi-name
.数据源的东西可以留在 application-dev.properties
中.
The solution is to create another properties file like application-prod.properties
and set the spring.datasource.jndi-name
there. The datasource stuff can stay in application-dev.properties
.
在您的 application.properties
文件中,您应该启用您喜欢使用的配置文件:spring.profiles.active=prod
或 spring.profiles.active=开发
In your application.properties
file you should enable the profile you like to use: spring.profiles.active=prod
or spring.profiles.active=dev
这篇关于SpringBoot:配置生产与开发环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!