It’s 131 days since Liu xiaoai taught herself Java.

Thank you for watching.


Today’s learning schedule is as follows:

  • Why use SpringBoot?
  • The power of SpringBoot.
  • Write a starter program with SpringBoot to learn how to use Java configuration.

Introduction of SpringBoot

Java, the programming language, has been criticized by many people for its bloated code and cumbersome use.

While we are still laboring to set up the project environment, Python programmers may have already written the code.

Believe it or not, it takes at least 20 minutes to build a complete SSM framework project environment from scratch.

There are two main reasons for this:

  • Complex configuration: configuration of various XML files, Spring, Mybatis, etc.

  • Messy dependency management: This is even more of a headache, deciding which dependencies to use in a project and resolving conflicting versions.

SpringBoot is designed to solve these problems.


SpringBoot is a sub-project of the Spring project, a member of the Spring family.

Boot means boot, and creating Java applications with SpringBoot is much easier and faster.

The main features of SpringBoot are as follows:

  • Have a very quick start experience.
  • There is absolutely no code generation and no XML configuration.
  • More advanced applications can be found in the official documentation.

Two, SpringBoot quick start

There are two ways to create maven projects: manually or automatically using scaffolding.

Both methods are very simple and we create them manually:


① Add parent project coordinates

We used to create a parent project by ourselves. Here is a parent project using SpringBoot:

spring-boot-starter-parent

Use SpringBoot to resolve version conflicts.

Version conflict is a very annoying problem, there are some bugs can not find the cause, may be the version conflict.

② Introducing dependencies

The parent project only acts as a version management function, and the specific dependencies that need to be used in the project need to be introduced.

In the preceding example, SpringBoot is automatically introduced based on the spring-boot-starter-web dependency, and all versions are managed without conflicts.


③ Write the startup class

Instead of either configuring Tomcat or using the Tomcat plug-in, tomcat can now be started directly in the main method.

So how do you do that?

  • Use @SpringBootApplication on the class.
  • Use springApplication.run () in the main method.
  • The argument is the Class object of the current Class.
  • Start the main method to run SpringBoot.

④ Write the Controller class

At sign RestController is the same thing as at sign Controller plus at sign ResponseBody, and we can look at the source code for that.


The @responseBody function is to return the value in JSON data format to the front end.

The return value is supposed to correspond to a view, such as a hello.jsp file, but now it is common to separate the front end from the back end. There is no front-end code in the back end, so the response data is converted to JSON and then the response.

(5) test

Enter the corresponding access path in the browser to access the corresponding method in the Controller class, and the page content is the Json data of the response.

Java configuration

Now that there are no XML files in the project, what do we do if we want to configure a Bean?

1 General Java configuration

Using Java configuration to solve this problem, let’s make a comparison between Java configuration and traditional XML configuration:


① Write the JDBC configuration file

Needless to say, the properties configuration file is required for all database configurations, whether XML or Java.

② XML configuration mode

The JDBCP configuration file is imported with the Contex :property-placeholder tag, and retrieved in ${} format.

Configure the Druid data source into the Spring container and assign the properties using the Property tag.

3. Java configuration

  • Configurarion indicates that this is a configuration class.
  • @propertysource Indicates importing a configuration file.
  • @value Indicates the Value in the configuration file.
  • @bean means stored in the Spring container.

The above are known by name, know the meaning of the word will understand its role.

2Java Configuration Method 1


(1) application. The properties file

This is the property file name that SpringBoot reads by default, so change the jdbc.properties name to that name.

② Automatic reading

Prefix =” JDBC “reads the values in the properties file with the prefix JDBC, so its four properties are injected.

This method is much simpler, often used, and seems super elegant, but it has some limitations:

Properties in a configuration file can only be used by one of its beans

So how do you make properties in a configuration file universal?

3Java configuration mode 2


① Configure a property reading class

The @ConfigurationProperties annotation on the class declares the current class as a property read class.

The member variables in the class must correspond to the property names in the configuration file, and must have both getXXX and setXXX methods.

I just used the Lombok plug-in for simplicity.

② The properties in the configuration file are universal

Any other class that wants to use a property from the configuration file, import that property to read the class, and get it using the getXXX method.

4 test

How do I test Java configuration properties without problems?


For our side, we can use the debug interrupt point method, enter the path in the browser, accept the request, and view the properties in the dataSource in the console.

If a result similar to the one above appears, the properties in the configuration file were successfully read.

The last

Thanks for watching.

If you can, please give it a thumbs up. Thank you.