Springboot study notes (2) multi-environment configuration of application

Introduction: Old programmers with certain development experience will have the feeling that the general database is not allowed to be moved during development, and you have to perform some testing and production, which is very inconvenient. In order to solve this problem The problem, springboot provides multi-environment configuration, that is to say, we can configure different environments in a project to solve the inconvenience caused by the above problems.

Tools: idea 7.3.1 jdk 1.8.0_172

step:

Scaffolding will no longer be built. It's very simple. Everyone knows it. If you can't, you can read my other articles: https://blog.csdn.net/qq_41441210/article/details/80225752

This new project relies on one more mysql than that one

01. Look at the project directory


A very simple helloworld, focusing on the following three configuration files

02. Look at the next three configuration files

The first is the configuration file of the production environment


Then the configuration file for the development environment


And finally the main config file


I wrote three configuration files, one is the production environment configuration file, his service port is 8081, the connection database is dbgirl, the other is the development environment configuration file, the service port is 8080, the connection database is dbtest, and finally the main configuration file, which sets the development environment

Assuming that my production environment database cannot be tampered with, and the development environment data is free to test data, then we are now in the development environment.

run it


Port 8081 can't work, port 8080 can work

I didn't write a test for the database, but since the project runs successfully, the database can be successful. If the database cannot be connected to the project log, an error will be reported.

If we don't need a development environment, need a production environment, can't tamper with the database, and are officially online, then the main configuration file will put

spring.profiles.active=dev

change to

spring.profiles.active=prod
That's it.

Moreover, in Linux, we use java -jar to run the project. As long as we add the prod of the production environment or the dev of the development environment, the project will run in a corresponding way.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813884&siteId=291194637