Narges
Narges

Reputation: 1355

How to change Spring Boot properties default path in Tomcat development environment

I need to use two *.properties file to determine config of Spring Boot application server. How can I set the second configuration path?

I use spring-boot-starter-parent version 1.5.10 and such *.properties file:

spring.datasource.url=url
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialec
spring.jpa.properties.hibernate.current_session_context_class=
org.springframewok.orm.hibernate4.SpringSessionContext
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.idle-timeout=30000

Now, I need to address database info from another properties file.

Edit: Note that I need to put second properties file outside of my WAR file in WEB-INF folder.

Upvotes: 0

Views: 2207

Answers (2)

sam
sam

Reputation: 2004

You can access multiple of properties file using @PropertySource

Follow the following stackoverflow thread. https://stackoverflow.com/a/47178674/7538821

Upvotes: 0

Vy Do
Vy Do

Reputation: 52516

(1) This is best practices when switching between development mode and production environment.

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles

Reference for version 1.5.10.RELEASE: https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-external-config-profile-specific-properties

For more specific, you create 3 files

application.properties for common properties.

application-dev.properties for only own properties what used in profile dev

application-production.properties for only own properties what used in profile production

(Notice: Has convention over naming)

Point profile what used, in application.properties has line spring.profiles.active=dev (in development) or spring.profiles.active=production (in production)

(2)

Note that I need to put second properties file outside of my WAR file in WEB-INF folder.

Assumption, your file is foo.properties. It is outside WAR file, it has not nature of Spring's properties file. Therefore, Spring Framework/Spring Boot can not read it automatically. You must write few lines of Java code to reading the content of foo.properties, then assign to configuration manually (see example configuration class)

Upvotes: 1

Related Questions