How to write a SpringBoot application? In fact, there is a pattern.

First, introduce scene dependencies

Since springBoot applications are to be developed, scenarios must be defined, such as web, caching, message queues, and so on.

Then we can find the relevant scenario dependencies based on the specific scenario, including the dependencies provided by SpringBoot and third-party scenario dependencies.

Then add it to pop.xml, such as spring-boot-starter-web.

2. View automatic configuration

This is not necessary and can be developed directly after introducing scenario dependencies. If you want to see which configurations are enabled, check them out.

Here’s a way to add a configuration to the application.properties configuration file to enable debug mode:

Debug =trueCopy the code

When you start the application, you’ll see more log output on the console, including the configuration that works and the configuration that doesn’t.

  • Negative matches: This is a list of configurations with inadequate conditions that are not enabled.

  • Positive matches: The configurations that meet the conditions are listed and are valid.

If you are interested in the effective configuration, you can analyze a wave by referring to the automatic configuration process you have learned before.

Three, whether to modify

1. Modify the configuration items according to the documents

After injecting dependencies, determine if there are configuration items that need to be modified.

For example, if you are using a database, you will naturally need to configure the user name, password, and so on for the connection.

When you modify the configuration, refer to the official documents to find the corresponding configuration items.

There are a lot of configuration items here. For example, let’s configure the banner displayed when the application starts. The default is this:

Now I’m going to replace it with another image. I’ll use the public account’s avatar and put the image banner1 in the Resources directory:

The Core was found in the document Properties in the spring. The banner. Image. The location.

To the application.properties file:

spring.banner.image.location=classpath:banner1.jpg
Copy the code

Start the app and see what it looks like:

The configuration is successful.

2. Add a user-defined component or replace the original component

If there are cases where the configuration changes are not enough, we can also customize the component to add our custom functionality by replacing the underlying component directly with our own @bean.

Although there is a lot more to SpringBoot, springBoot is basically ready to use after completing the above steps.