How to deploy the same spring boot Web application to a different environment

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.