JAVA distributed -- SpringbootSpringboot quick start, core configuration file interpretation, directory interpretation, custom configuration items, @ Value annotation @ configurationproperties

Posted by trg on Sat, 19 Feb 2022 21:55:01 +0100

1, springboot Basics

1.1 general

Spring Boot can easily and quickly create applications based on the spring framework, which enables coding, configuration, deployment and monitoring. Spring Boot has attracted much attention in the industry because it simplifies the complexity and makes the development extremely simple and fast.

1.2 characteristics

  • Ability to quickly create Spring based applications
  • You can directly use the java main method to start the embedded Tomcat server and run the Spring Boot program without deploying the war package file
  • Provide the agreed starter POM to simplify Maven configuration and make Maven configuration simple
  • Automatic configuration: according to Maven dependency configuration of the project, Spring boot automatically configures ssm, etc
  • It provides functions such as health check of the program
  • Basically, you can use annotation configuration without using XML configuration files at all

1.3 four cores

  • Automatic configuration (key)

  • Start dependence (key)

  • Actor (non key)

  • Command line interface (non key)

2, Building the springboot framework

2.1 create a Module and select Spring Initializr for quick build

2.2 setting GAV coordinates and pom configuration information

2.3 select Spring Boot version and dependency

2.4 set the module name, Content Root path and directory of module files

They are: set the module name, set the module root directory, and set the template file directory

2.5 creation completed

3, Explanation of configuration file

3.1 form of test and coding

  • The Application in the springboot package is the entry point of the program

  • All classes must be used in the same directory or subdirectory of Application

3.2 pom.xml file interpretation

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
	<!--inherit SpringBoot A parent project of the framework, all self-developed Spring Boot Must be inherited-->
		<relativePath/> <!-- lookup parent from repository -->
	<!--Of the current project GAV coordinate-->

	<!--maven Project name, which can be deleted-->
	<!--maven Item description, which can be deleted-->
	<description>Demo project for Spring Boot</description>
	<!--maven Property configuration, which can be used in other places ${}Reference by-->
	<!--Compiling environment-->

		<!--Springboot frame web Start dependent project-->
		<!--springboot Framework test start dependency-->

			<!--springboot Project packaged and compiled plug-ins-->


3.3 catalog interpretation

  • .mvn|mvnw|mvnw.cmd: script operation is used to execute maven related commands. It is rarely used in China and can be deleted
  • . gitignore: when using the version control tool git, set some content to ignore the submission
  • static|templates: the directory where files are stored in the template technology
  • the configuration file of SpringBoot. Many integrated configurations can be found in this file
    Configure, such as Spring, Spring MVC, Mybatis, Redis, etc. Is currently empty
  • the entry for the execution of the SpringBoot program. Execute the main method in the program, and SpringBoot will start

3.3 core configuration file properties

  • This file is used by default
  • By modifying application The properties configuration file is used to modify the default tomcat port number and the configuration method of the properties property file of the root key value pair of the upper and lower files of the project

3.4 core configuration file yml file

  • yaml is an intuitive data serialization format that can be recognized by computer, which is easy to be read by human beings
  • Yaml is similar to xml, but its syntax is much simpler than xml. There must be a space between the value and the colon configuration item in front. The yml suffix can also be used after yaml

3.5 do the properties and yml files exist at the same time?

  • First of all, the functions of the two files are the same

  • When the two format configuration files exist at the same time, the properties configuration file

3.6 multi environment configuration (properties are the same as yml file)

  • In the actual development process, the project will go through many stages (Development - > test - > online)
  • The configuration of each stage is also different, such as port, context root, database, etc
  • In order to facilitate switching between different environments, SpringBoot provides multi environment configuration

Step 1: create a configuration file for each environment. The name must be identified by application environment properties|yml

Step 2: in the general configuration file application Properties to activate the environment

4, Custom configuration item

In the core configuration file of SpringBoot, in addition to using built-in configuration items, we can also customize the configuration, and then use the following annotation to read the attribute value of the configuration

4.1 use @ value annotation

  • This annotation is mainly used when there are few user-defined configuration items, and can be assigned one by one
  • properties file
  • yml file

Use @ Value() annotation

public class IndexController {
    private String school_name;
    private Integer age;
    @RequestMapping(value = "/hello")
    public String hello(){
        return "hello world"+school_name;

Debugging results

4.2 use @ ConfigurationProperties

Map the whole file into an object, which is used when there are many custom configuration items

4.2.1 cases

  • properties file

  • Create an object to associate this configuration file

  • Test and access

  • The annotation must be used with a prefix. Otherwise, it cannot be used. And the prefix is uniform

4.3 warning resolution

  • The following warning will appear at the top of the newly created class using the above annotation:
  • In POM XML add the following configuration to eliminate
<!--Solve use@ConfigurationProperties Warning problem with annotation-->
  • Warning elimination

4.4 Chinese garbled code solution

If there is Chinese information in the SpringBoot core configuration ( file, garbled code will appear

  • Generally, Chinese is not recommended in the configuration file (except notes)
  • If yes, it can be converted to ASCII code first

Topics: Java Maven Spring Spring Boot