In real-world projects we often have different deployment environments, such as the Dev database, the system test database, and the production database, so how do you deploy the same spring boot Web app to a different database environment?
Spring boot provides a profile feature that enables the same application to switch to a different deployment environment by configuring multiple profiles. The concept of profile is not described in detail here. Interested in self-check on the official website. Here is a code to show how to configure spring boot to implement spring JDBC Tempalte switching between different JDBC data sources
1 introduction of necessary dependencies in Pom.xml
<dependency> <groupid >org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> < groupid>org.springframework</groupid> <artifactId>spring-jdbc</artifactId> </dependency> < dependency> <groupid>com.oracle</groupid> < Artifactid>ojdbc6</artifaCtid> <version>11.2.0.3</version>
Note: I am using Oracle JDBC here.
2 Create a different spring boot configuration file, these three file tables represent dev,system,production
Application.propertiesapplication-sys.propertiesapplication-prod.properties
Note Application-{profile} corresponds to a different environment. Different profiles can be toggled via Java-jar-dspring.profile.active=sys
3 Configuring different JDBC information into the appropriate configuration file
In the Application.properties file Spring.datasource.url=jdbc:oracle:thin: @dev01. example.com:1521: Lausonedspring.datasource.username=devuserspring.datasource.password=xxxxxspring.datasource.driver-class-name= Oracle.jdbc.OracleDriver in Application-sys.propertiesspring.datasource.url=jdbc:oracle:thin: @sys01. example.com : 1521:lausonedspring.datasource.username=sysuserspring.datasource.password= Xxxxxspring.datasource.driver-class-name= Oracle.jdbc.OracleDriver in application-prod.propertiesspring.datasource.url=jdbc:oracle:thin:@ prodv01.example.com:1521:lausonedspring.datasource.username=produserspring.datasource.password= Xxxxxspring.datasource.driver-class-name=oracle.jdbc.oracledriver
4 Configuring Spring Boot
@Configurationpublic class Databaseconfig {@Bean (name= "Asudbsource") @ConfigurationProperties (prefix= " Spring.datasource ") Public datasource Primarydatasource () {return datasourcebuilder.create (). build ();} @Bean (name =" A Sujdbc ") @Autowired public JdbcTemplate blcjdbctemplate (@Qualifier (" Asudbsource ") DataSource source) {return new JdbcTemplate (source); }}
Here I create a Databaseconfig.java file to configure the JDBC connection to the database and return the data source, and configure the data source to return JdbcTemplate. I'm using a traditional JDBC connection database and I'm not using hibernate.
5 Call JdbcTemplate via Dependency injection (DI).
@Repositorypublic class Asudaoimpl implements Iasudao {@Autowired @qualifier (value= "ASUJDBC") Private JdbcTemplate JdbcTemplate;}
Here I create a DAO interface, and DAO Impl to inject this jdbctemplate.
This article is from the "Development" blog, make sure to keep this source http://jamesdev.blog.51cto.com/2066624/1865250
How to deploy the same spring boot Web application to a different environment