Get the App
SLTechnology News&Howtos  ›  Database  › 

How to deploy the same Spring boot web application to different environments

Shulou Source: shulou.com Published: 2022-06-01 17:41:32 09月18日 Update

In real projects, we often have different deployment environments, such as dev database, system test database and production database, so how to deploy the same spring boot web app to different database environments?

Spring boot provides the function of a profile, which enables the same application to switch to different deployment environments by configuring multiple profile files. The concept of profile is not covered in detail here. If you are interested, go to the official website to inquire. Here is how to configure spring boot to enable spring JDBC tempalte to switch between different jdbc data sources through code.

1 introduce necessary dependencies in pom.xml

Org.springframework.boot spring-boot-starter-jdbc org.springframework spring-jdbc com.oracle ojdbc6 11.2.0.3

Note: I am using oracle jdbc here.

2 create different spring boot configuration files, these three file subtables represent dev,system,production

Application.propertiesapplication-sys.propertiesapplication-prod.properties

Note that application- {profile} corresponds to different environments. You can switch different profiles through java-jar-Dspring.profile.active=sys

3 configure different jdbc information to the corresponding 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 configure spring boot

@ Configurationpublic class DataBaseConfig {@ Bean (name= "asuDBsource") @ ConfigurationProperties (prefix= "spring.datasource") public DataSource primaryDataSource () {return DataSourceBuilder.create () .build ();} @ Bean (name= "asuJDBC") @ Autowired public JdbcTemplate blcJdbcTemplate (@ Qualifier ("asuDBsource") DataSource source) {return new JdbcTemplate (source) }}

Here I created a DataBaseConfig.java file to configure jdbc to connect to the database and return the data source, and configure the data source to return jdbcTemplate. I use traditional jdbc to connect to the database and do not use hibernate.

5. Call JDBCTemplate. 5 through 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.

Tags: Data configuration different file database environment data source switch application three code representative tradition information interest sub-table function can be adopted multiple concept Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno macOS MariaDB Linux