问题
I am trying to configure multi deployment environment for my web application using Spring Boot with Maven. Created several .properties files under src/main/resources/config. db-dev.properties and db-prod.properties consist of db specific information:
db.url=jdbc:oracle:thin:@ldap://dev.com/risstg3,
db.username=owner
db.password=godzilla
In the same directory I also have application.properties which reads in the variables defined in these db property files
#database info
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=${db.url}
spring.datasource.username=${db.username}
spring.datasource.password=${db.password}
#hibernate config
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
Multiple profiles are set up in my pom:
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
<build>
<filters>
<filter>src/main/resources/config/db-${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources/config</directory>
<filtering>true</filtering>
</resource>
</resources>
All other configuration is taken care of by Spring Boot with @SpringBootApplication annotation in my application class.
Then I built the war by specifying profile using -P option, e.g. mvn install -Pprod
. However I failed to deploy it with tomcat on local machine due to the following error:
SEVERE: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ristoreService]]
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
I thought I set 'hibernate.dialect' in application.properties with spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
Based on this thread, this error may not necessarily have to do with hibernate dialect. You may see it if the db connection is not successful. What did I miss? Is there a way to tell whether the war is created with the correct profile and whether the variables defined in db-{dev}.properties are picked up?
回答1:
This is a slight variation to your setup but I think would help solve the problem in a more Spring friendly way. Spring has the concept of profiles. You can create application.properties
files as your default settings and then have application-${profile}.properties
for your profile specific settings. If you create an application-dev.properties
and an application-prod.properties
all you need to do is then specify an environment variable called spring.profiles.active=dev
and that would use those. This way you don't need to create separate jar files for each type of deployment.
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-change-configuration-depending-on-the-environment
来源:https://stackoverflow.com/questions/39091901/maven-application-with-multi-environment-configuration-cant-deploy-on-tomcat