Hello! I am Xiaoxiao, today is the fifth part of this week, and the fifth part of this week will focus on the Hello World of Spring Boot
Preface
Reasons for Spring Boot
The emergence of Spring Boot is mainly used to solve some problems of Spring in the past. It puts forward the idea of convention over configuration. By default, many methods are set so that developers can quickly build projects and integrate third-party content. Makes development efficiency greatly improved.
basic concept
Spring Boot is not only a set of frameworks, but also a set of systems. The purpose is to simplify Spring development.
Features
Spring-based development provides a faster entry point to get started directly, redundant code does not have an embedded container to simplify Spring
core function
Extremely dependent on build tools for automated configuration
Hello World
Maven create
Create a new empty project and create modules respectively, as shown in the figure below
Create Maven Module
Create a Module, select the Maven project, check the previously used web skeleton and fill in the GroupID. After the ArtifactID is selected, press Enter to complete the creation of a basic maven project
Add start-up dependency
According to the requirements of Spring Boot, perform simple tests and add corresponding start-up dependencies. You need to inherit the start-up dependency of Spring Boot. Spring boot starter parent In order to integrate Spring MVC for Controller development, you need to import Spring boot starter web
<?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.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
</parent>
<groupId>cn.ideal</groupId>
<artifactId>springboot_01_start</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Write Spring Boot startup class
Write Spring Boot startup class here
package cn.ideal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class);
}
}
Create a control layer
package cn.ideal.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class QuickStartController {
@RequestMapping("/test")
@ResponseBody
public String test(){
return "springboot 访问测试,起飞,飞飞飞飞 ~ ~ ~";
}
}
Test Spring Boot
The project starts, the console will output the following
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.7.RELEASE)
2020-05-10 22:11:34.973 INFO 30580 --- [ main] cn.ideal.MySpringBootApplication : Starting MySpringBootApplication on LAPTOP-5T03DV1G with PID 30580 (F:developIdeaProjectsframework-codespringboot_01_demospringboot_01_starttargetclasses started by abc in F:developIdeaProjectsframework-codespringboot_01_demo)
2020-05-10 22:11:34.976 INFO 30580 --- [ main] cn.ideal.MySpringBootApplication : No active profile set, falling back to default profiles: default
2020-05-10 22:11:35.686 INFO 30580 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-05-10 22:11:35.693 INFO 30580 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-05-10 22:11:35.693 INFO 30580 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.34]
2020-05-10 22:11:35.765 INFO 30580 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-05-10 22:11:35.766 INFO 30580 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 747 ms
2020-05-10 22:11:35.884 INFO 30580 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-10 22:11:35.990 INFO 30580 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-10 22:11:35.993 INFO 30580 --- [ main] cn.ideal.MySpringBootApplication : Started MySpringBootApplication in 1.318 seconds (JVM running for 2.665)
The picture shows the following input and the created controller project is directly printed out
The project is packaged into a jar package
Add dependency
<plugin>
<!-- 打包插件 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Click on the right, package for packaging, select the package option
You can see that a new jar package is generated under target, which is run directly in cmd
About the author
I am a little, a little programmer. bye~bye!
Xiaoming Vegetable Market
Recommended reading
● Very good | This article is well written! Spring Boot + Redis realizes interface idempotence
● Mining business started! Xiaoxiao takes you money and takes you to fly!
● Finance | October, my financial situation
● Tuning | Don’t say you don’t know how to monitor and tune JVM performance
● Unexpectedly | Never thought that the most important keyword in Java turned out to be this
Give me a nice look, okay?