Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Spring boot Learning Series (2)

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Spring boot multi-context configuration and yml profile

1. In peacetime projects, we may need to configure the production environment, test environment, and development environment.

2. Every time the project is released, you may need to change the configuration file and modify some paths.

3. Next, let's talk about the multi-environment configuration of spring boot and the yml configuration file.

4. Based on the previous demo project. Let's open the application.properties file.

You can see that it is empty and there is nothing inside

6. Let's modify the port number and add

Server.port=8088

Then we start the access, and the port is modified successfully.

7. Let's take the port as an example.

The production environment is changed to 80, the test environment to 8088, and the development environment to 8080

The three new files are:

8. Then write the contents.

Application.properties content:

Spring.profiles.active=dev

The dev here indicates that the environment is application-dev

Application-dev.properties development environment content:

Server.port=8080

Mybatis.type-aliases-package=com.example.demo.entity

Spring.datasource.driverClassName = com.mysql.jdbc.Driver

Spring.datasource.url = jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8

Spring.datasource.username = root

Spring.datasource.password = 123456

In addition to the port number, add the database access address.

Application-pro.properties production environment content:

Server.port=80

Mybatis.type-aliases-package=com.example.demo.entity

Spring.datasource.driverClassName = com.mysql.jdbc.Driver

Spring.datasource.url = jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8

Spring.datasource.username = root

Spring.datasource.password = 123456

The contents of the application-test.yml test environment:

Server: port: 8088mybatis: type-aliases-package: com.example.demo.entityspring: datasource: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8 username: root password: 123456

Is yml content more comfortable and pleasing to the eye than properties?

If you take a closer look at the data structure, you can probably see the difference between yml and properties. In fact, the data read is the same. It's just the format is different.

9. The configuration is almost like this. Let's try to run and see how it works.

1)

Start the dev environment first:

Take a look at the environment in the application.properties file.

Launch to see what the port number is after using dev.

Yeah, port 8080, it's fine.

2)

Reboot for pro in the modified application.properties file.

Restart and try to access port 80

Well, there seems to be nothing wrong with it. Visit 8080 to see if you can still access it.

Port 8080 can't be accessed. That's reassuring.

3)

Finally, change the application.properties file to test.

Start the interview and have a look.

Well, there seems to be nothing wrong with it.

Almost, that's it. The next article will talk about spring boot integrating mybatis.

The typhoon is coming, don't be blown away.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report