Spring Boot JPA Database Choice

How can I start a stand-alone Spring Boot JPA application -- not via cli -- with a choice of databases to get data, eg, localhost:5432/my_db; or 192.168.1.100:5432/our_db, or example.com:5432/their_db?

Mine currently uses the one in the application.properties file that contains:

spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/my_db
spring.datasource.username=postgres
spring.datasource.password=postgres

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

Thanks in advance


Since you probably need to configure username and password as well, I recommend creating separate application-mydatasource.properties files for each data source configuration. You will then activate the datasource you want to use based on setting the active profile. You can set the active profile either in application.properties ( spring.profiles.active ) or via a command line argument:

$ java -jar -Dspring.profiles.active=mydatasource demo-0.0.1-SNAPSHOT.jar

The application-mydatasource.properties will then override any properties in your application.properties . I believe you will also need to set spring.profiles= to the list of profiles available.

See Profile specific properties.


Another options besides the @Profile label, that you will have to declare in every enviroment that you will deploy the application, you could use in Spring Boot the label:

@ConditionalOnProperty(name="propertyName", havingValue="propertyValue")

And declare a property to decide wich database you want to load in each case!

Hope being helping!!

链接地址: http://www.djcxy.com/p/22466.html

上一篇: angular:使用restmod作为restangular的替代方案,用于休息操作

下一篇: Spring启动JPA数据库选择