🚀 Quality resource sharing 🚀
Learning route guidance (click to unlock) | Knowledge orientation | Crowd positioning |
---|---|---|
🧡 Python actual WeChat ordering applet 🧡 | Progressive class | This course is a perfect combination of python flask+WeChat applet, from project construction to Tencent cloud deployment and online, to build a full stack meal ordering system. |
💛 Python quantitative trading practice 💛 | beginner | Take you hand in hand to build a quantitative trading system that is easy to expand, safer and more efficient |
Spring Boot 2.7.2 learning series. This section gives you a quick experience of Spring Boot and introduces you to its basic use, operation and packaging.
Spring Boot is based on the Spring framework, and the underlying layer cannot be separated from IoC, AoP and other core ideas. Spring 4.0 provides a development method based on Java Config. Spring Boot came into being at the historic moment, which can simplify the development process of Spring applications, and also can quickly and easily integrate third-party frameworks, such as MyBatis, Redis, etc.
0 Version Description
- Development tool IDEA version: 2021.2
- Maven Version: 3.6.3
- Spring Boot Version: 2.7.2
- JDK version: JDK 8
- MySQL version: MySQL 8
Note: The latest stable version of Spring Boot 2.x is 2.7.2, JDK 8 requires more than 3.5, and Maven requires more than 3.5. (It was originally intended to be based on Spring Boot 3.x, but 3.x requires Java 17, and the elegant computer is only JDK 8)
1 Create Spring Boot application
1.1 Create Project
1) Open idea, create a Maven project, and click "Next"
2) Enter Name, GroupId, ArtifactId, and click Finish
1.2 Setting IDEA
Set JDK and Maven in IDEA Preferences
1) Set Maven
2) Set JDK
Set JDK in Module Settings
Set SDKs first:
Then select the SDK set in "Project"
Finally, select Language Level in Modules:
2. Add Spring Boot support
2.1 Adding Dependencies
1) In pom Add Spring Boot dependency in xml.
In the articles and blogs of Bad Street, spring boot starter parent is inherited through parent, as follows:
<parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.7.2version> parent>
This method is almost used in demo writing. It is rarely used in large projects or medium and large enterprises. Because each module can only have one parent, while in enterprise development, microservices have multiple services, which are generally inherited from a unified module, facilitating version control and general functions. If spring boot starter parent occupies the parent node in each service, how can we inherit the unified parent module?
Some people may say that spring boot starter parent is inherited from the parent module. Yes, it can. However, in addition to services, some public modules (such as parameter verification, general response, distributed Redis lock, Spring Doc and other general modules) also inherit from this parent module. In this way, these public modules are also forced to add spring boot starter dependencies.
I do this through spring boot dependencies:
- Define the spring boot dependencies version number in properties
- In dependency management, add spring boot dependencies for import through pom and scope
- Add the dependencies to be used in dependency dependencies.
pom. The xml file is as follows:
xml version="1.0" encoding="UTF-8"? <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>com.yygnb.demogroupId> <artifactId>hero-springboot-demoartifactId> <version>1.0-SNAPSHOTversion> <properties> <maven.compiler.source>8maven.compiler.source> <maven.compiler.target>8maven.compiler.target> <spring-boot-dependencies.version>2.7.2spring-boot-dependencies.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-dependenciesartifactId> <version>${spring-boot-dependencies.version}version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> project>
2.2 Creating a Startup Class
Create startup class: com.yygnb.demo.DemoApplication
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
3 Test run
3.1 Adding a Test Controller
Create the Controller used by the test: com.yygnb.demo.controller.DemoController
@RestController @RequestMapping("demo") public class DemoController { @GetMapping("hello") public String hello(String msg) { String result = "Hello Spring Boot ! " + msg; System.out.println(result); return result; } }
3.2 Startup and operation
Run the main method in DemoApplication to start the application. If the console displays the following, the application starts successfully:
Access in browser:
http://localhost:8080/demo/hello?msg=FirstTest
Both the page and the console display:
Hello Spring Boot ! FirstTest
3.3 Modifying Ports
The default running port of Spring Boot is 8080. If you want to modify the running port number, you need to modify the configuration implementation. Spring Boot supports configuration files in three formats: xml, yml, and yaml. The yml format is widely used in projects.
Note when using the yml format:
1. Use colons to separate property names and values 2. Character case sensitivity 3. Indent sensitive between levels (two spaces apart) 4. A space is required before the attribute value
The Spring Boot core configuration file is named application.
Create the configuration file application.yml in src/main/resources
server: port: 9099
This configuration specifies that the port number of the service to run is 9099. Restart the service and change the 8080 in the above access path to 9099.
4 Packaging
The Maven project is packaged through the mvn command. Execute the following commands under the project root directory in the console:
mvn clean package
After executing this command, the hero-springboot-demo-1.0-SNAPSHOT will be generated in the target directory jar. The size of the file is only 4kb. This is because direct packaging will only package the current code, and the spring boot dependencies used will not be included. At this time, the jar file cannot be run directly. If you want the packaged jar to run directly, you need to run it in pom Configure relevant plug-in configurations in xml:
4.1 Configuring plug-ins
<build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> <version>${spring-boot-dependencies.version}version> <executions> <execution> <goals> <goal>repackagegoal> goals> execution> executions> plugin> plugins> build>
Re execute mvn clean package, and two files will be generated in target:
- hero-springboot-demo-1.0-SNAPSHOT.jar.original
- hero-springboot-demo-1.0-SNAPSHOT.jar
The former is a packaged original file, which contains only the code of the current project as before configuring the plug-in. The latter includes spring boot dependencies. Tomcat is built in and can be run directly.
4.2 Running jar
The executable file generated by the above package can be executed through the java command. In the console, execute:
java -jar target/hero-springboot-demo-1.0-SNAPSHOT.jar
After the command is executed, the service will still be started.
This article records another way to rely on spring boot. In the next chapter, MyBatis Plus is integrated to implement addition, deletion, modification and query.
Today, the learning of youyacoder is over, and we look forward to sharing your comments~~