“This is the 22nd day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Hi, family, I’m bug bug, here I go again. Today we are going to talk about something, OK, and we will continue the series of articles about Springboot. I hope you found this series useful!

It has good teaching value. I hope you can gain something from this article. I suggest you collect it first and then read it.

If you think the article is good, you are welcome to like it, bookmark it, comment on it, and share it with us. Remember to give the bug bacteria a one-button three-link ~~

Ok, let’s start the main body of this issue.

One, foreword

In actual project development, a project usually has multiple environments, such as development environment, test environment and production environment. The configuration varies from environment to environment, for example, the development environment uses a development database, the test environment uses a test database, and the production environment uses a formal database online.

So the question is, every time you release a test environment or go into production, how do you configure the configuration environment? Isn’t it? Don’t worry, SpringBoot does it all for us, we just need to know how to use profile!

Profiles have been around since Spring3.1. What is a profile? It is the ability to enable Spring to provide different configurations for different environments, quickly switching environments by activating, specifying parameters, and so on.

** In a nutshell: ** is that we need to use different configurations in different scenarios, profile was born to solve the complexity of switching configurations in multiple environments.

How to use profile?

Spring Boot configuration files come in two forms:.properties files and.yml files. Either of these files can be named to distinguish the configuration of different environments.

application-{profile}.properties/yaml
Copy the code

{profile} is usually the name or abbreviation of each environment, such as dev, test, and Pro. Here we use the YAML configuration file format directly for the demonstration:

Yaml configuration

Add the following four configuration files to the Config folder of the Demo project:

  • Application. yaml: master configuration file
  • Application-dev. yaml: development environment configuration file
  • Application-test. yaml: indicates the test environment configuration file
  • Application-pro. yaml: production environment configuration file

In the application.yaml file, specify the default server port number 8080 and activate the development environment (Dev) profile with the following configuration.

When you start the project without specifying the environment, you can see the console print:

No active profile set, falling back to default profiles: default
Copy the code

Looking at what I’ve circled, it’s clear that there are openings for configuration. How should it be used? Don’t worry, look down.

As an experiment, we set the properties in the core configuration file application.yaml. Spring.profiles. active=dev, then start the application and find that application-dev.yaml is activated and the startup port is also changed to the port number set in the child configuration file.

Look at the console print and test:

You can clearly see that the DEV environment has started.

This time, port 8090 is configured in the test environment. Let’s see what the result will be.

The application-test.yaml content is set as follows:

server:
  port: 8090
Copy the code

The application.yaml content is set as follows:

spring:
    profiles:
      active: test
Copy the code

Can it finally start the project successfully and run port 8090? Or run port 8080 with no specified environment? Then look down;

Ok! As you can see, it is the tests environment configuration that is running. This verifies that profileh was successfully used to dynamically switch configurations during the two Application.yaml tests.

The configuration mode is dynamically switched through the spring.profiles. Active property of the configuration file. Is there any other boot mode? Don’t worry, there must be, look below, bug bacteria are summed up for everyone, can learn ha ~

You can activate the configuration file in either of the following ways:

Active = dev(demonstrated above) VM parameter configuration: Specify in VM options: -dspring.profiles. active = dev Command line parameters: Java -jar xxx.jar --spring.profiles.active = devCopy the code

The.properties file is also configured in the same way as the.yaml file, which I won’t go into here. You can try it yourself, any doubt can be directly below the message oh ~bug bacteria will spare no effort to help you. Give a rose, the hand has lingering fragrance.

I’ll show you and use the remaining two ways to activate the configuration

3. Configure VM parameters

Open the RunDebug Configurations screen in the IDEA development tool; Then specify it in the VM Options line;

-Dspring.profiles.active=dev
Copy the code

Specify dev environment; Dev can be changed to another environment. Just change it to another environment.

application-dev.yaml

server:  port: 8100
Copy the code

The project started normally. Note VM parameters can be configured to dynamically switch the environment.

4. Configure command line parameters

Start by putting our demo project into a JJAR package

First teach everyone how to use IDEA jar package, in fact, very simple, see the following steps in an instant you can learn.

[INFO] BUILD SUCCESS
Copy the code

If “SUCCESS” is printed, the JAR is successfully packaged.

Then we find the location of the JAR; Save to target root directory

To jump to the jar file, right-click to open the panel and select Show in Explorer.

The following is the location of the file, then directly enter CMD in the directory, you can open the small black box.

Run the following command in the small black box and see if it works. We shall see;

Java jar demo - 0.0.1 - the SNAPSHOT. Jar -- spring. Profiles. The active = testCopy the code

— Spring.profiles. Active =test is the command line parameter to activate the test environment (test) Profile.

The running results are as follows:

Ok, perfect, successful run! This proves that the cli parameters can also dynamically switch the environment.

As to everybody how to choose, this sees your actual demand!

OK, that’s all for this episode. If you have any questions, feel free to comment in the comments section. See you next time.

Five, hot article recommendation:

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Springboot< a >: zero basic entry Springboot and IDEA setup

Springboot< 2 >: zero basic entry yamL, properties configuration file introduction and actual use

Springboot< three >: multi-environment switch, profile example demonstration

Springboot< four >: Introduction to stater teaching

Springboot< 5 >: common annotations for Springboot

Springboot< six >:mysql configuration and database query, implement add, delete, change and check

Springboot< seven >: Mybatis -plus entry and actual use to achieve the increase, deletion, change and check

Springboot< 8 >: Mybatis -plus conditional constructor user manual

Springboot< 9 >: Mybatis – Plus custom SQL zero-based teaching

Springboot< ten >: Mybatis XML mapping file >, <= and other special symbols written

. .

If you want to learn more, you can pay attention to the bug bug column “SpringBoot Zero-based Introduction”, from scratch, from zero to one! Hope I can help you.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Be who you want to be, there is no time limit, you can start whenever you want,

You can change from now on, or you can stay the same. There are no rules, and you can be the best version of yourself.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

If this article has been helpful, please put your thumb in the bottom left corner of the article. (# ^. ^ #);

If you like the articles shared by Bug bug, please send bug bug a note! The danjun ‘ᴗ, you guys will have a cameo appearance with you.

If you have any questions about this article, please leave a comment below or join the group [Group number: 708072830].

In view of limited personal experience, all views and technical research points, if you have any objection, please directly reply to participate in the discussion (no offensive comments, thank you);

Copyright notice: This article is the blogger’s original article, reprint please attach the original source link and this article statement, all rights reserved, piracy will investigate! (* ^ del ^ *)