Table of contents
1. What is springBoot? Why learn springBoot
Advantages of springBoot: (5 points)
Advantage 1: Fast integration framework
Advantage 2: Built-in Tomcat container
Advantage 3: Quickly deploy projects
Advantage 4: less configuration, more annotations
Advantage 5: Support more monitoring indicators
Second, the creation of springBoot project
Step Two: Add Some Framework Support
Step 3: View the status of the project creation
The relationship between the startup class and other classes
3. SpringBoot configuration file
3.1 The role of the configuration file
3.2 Format of the configuration file
Classification of configuration files
Format of the configuration file
Rules of springBoot configuration (format? Conflict?)
Syntax of the properties configuration file
How to read the contents of the configuration file
Properties Disadvantage Analysis
yml configuration file description
yml for single and double quotes
What is the difference between properties and yml (important)
Fourth, specify the file to read
@PropertySource reads the configuration file
1. What is springBoot? Why learn springBoot
When learning spring before, we learned that the birth of the spring framework can simplify the development of Java programs .
The translation of springBoot is the scaffolding of the spring framework .
Advantages of springBoot: (5 points)
Advantage 1: Fast integration framework
springBoot provides various start-up dependent functions for quickly integrating various frameworks;
Advantage 2: Built-in Tomcat container
There is a built-in running container, no need to configure additional web containers such as Tomcat .
SpringBoot has a built-in Tomcat container packaged as a jar package , and its API will be called when it needs to be used.
Advantage 3: Quickly deploy projects
You can start and run the project without an external container. When the springBoot project starts, you only need to write a main method , and then run the main method.
In the previous Servlet era, you also need to install the smart Tomcat plug-in. The tedious integration and deployment operations are required to run the project.
Advantage 4: less configuration, more annotations
springBoot does not require too many configuration files, it only needs to configure the data source when connecting to redis or mysql.
Advantage 5: Support more monitoring indicators
Through these indicators, you can better understand the operation of the project. (such as which beans are there, etc.)
Disadvantages of Spring Boot
The version update is too fast, and the module difference between different versions is relatively large.
Second, the creation of springBoot project
If the idea editor is a professional version, click on the upper left corner and directly select New spring initializr .
Step 1: Create a new project
Step Two: Add Some Framework Support
Step 3: View the status of the project creation
See a startup class, indicating that it has been created successfully
Step 4: Run the project
Run the main method of the startup class. If you see the spring logo output, it means the operation is successful.
SpringBoot project structure
The relationship between the startup class and other classes
Other classes must be in the same directory as the startup class . (That is, it must be under the same package as the startup class, or under the subpackage of the startup class ), so that other classes can be scanned.
3. SpringBoot configuration file
3.1 The role of the configuration file
store important information
All important data of the entire project are written in configuration files , for example:
Database connection information, project startup port, log information of third-party systems, etc.;
If there is no configuration file, then the project cannot connect to the database .
Reduce code coupling
For information such as database connections, it should not be written in the Java code, but should be written in the configuration file .
scene 1:
At the same time, it is also convenient for developers to modify the content of the configuration file .
(If it is written in the .java file, then for the compiled .class file , it is a read-only file and cannot be modified )
Scenario 2:
It is convenient for operation and maintenance personnel to view the servers, database connections, etc. that the project needs to deploy.
3.2 Format of the configuration file
Classification of configuration files
1. System configuration files: such as database connection strings , log related configurations, etc., which are defined by the system.
2. User-defined configuration files. For example, some files that the user wants to read
Format of the configuration file
Generally, there are two types, one is .properties and the other is .yml
However, no matter which format, it must be named application.yml or application.properties
Because springBoot only reads the configuration file named application by default when it starts .
Rules of springBoot configuration (format? Conflict?)
Rule 1: There can be two configuration files in a project (one is properties, the other is yml), but it is not recommended to have both .
Rule 2: If the content of the .properties and .yml configuration files conflict, how will it be handled? Preference is given to reading .properties configuration files.
Syntax of the properties configuration file
Properties are configured in the form of key-value pairs .
At the same time, you can also customize the content of the configuration file in properties
How to read the contents of the configuration file
Use the @Value annotation to read the content of the configuration file: the value followed by the server.port attribute
/**
* @author 25043
*/
@Component
public class ReadProperties {
/**
* value当中的值为配置文件当中
* server.port的值
* 一定要加大括号
*/
@Value("${server.port}")
private String port;
@PostConstruct
public void postConstruct(){
System.out.println("yml当中的值:"+port);
}
}
Properties Disadvantage Analysis
For some keys, it will have a lot of redundant content:
When multiple keys have the same part, it is necessary to list the same parts one by one, which is redundant.
yml configuration file
yml configuration file description
Yml is the abbreviation of YAML, its full name is: "Yet another Markup Language", yml is a highly readable, easy-to-understand format for expressing data serialization.
The biggest advantage: it can be cross-language.
yml features:
1. Simple writing and high readability;
2. Support more data types (;
3. Support cross-language use: java/python
yml syntax
This is also stored in the form of key-value .
But its writing method is more concise than properties.
When there are duplicate elements in multiple keys, the same parts of these keys can be extracted.
properties:
yml for single and double quotes
yml configuration object
Multi-line writing:
#对象写法1
student :
id : 1
name : 张三
age: 22
Single-line writing:
#对象写法2
student2 : {id : 2, name : 李四, age : 20}
Get object:
Step 1 : Store an object in properties:
Step 2 : Create a new entity class with exactly the same properties as this object
Step 3 : Inject this entity class into the Spring container (5 categories of annotations)
Step 4 : Get the object from the configuration file
Step 5 : Get the object from via property injection
yml configuration collection
Configuration file:
#dbtypes对应一个实体类
#name对应的是集合
dbtypes : { name: [ mysql, sqlServer,db2 ] }
Inject collection:
Get beans:
What is the difference between properties and yml (important)
Difference 1 : properties only support Java projects, but yml supports multiple languages including Java (such as python);
Difference 2 : The key of properties is more redundant to write, not as simple as yml. (yml supports levels, and indentation is used between levels )
Difference 3 : yml supports more data types (string, object, list)
Fourth, specify the file to read
@PropertySource reads the configuration file
This annotation can act on the class and specify which configuration file to read .
If there is no such annotation, the @Value annotation reads the contents of the application.properties file by default.