Springboot artifact quick start articles, with pictures and texts

image-20200809125922162
The article has been hosted on GitHub . You can check it out on GitHub. Welcome bosses to Star!
Search and follow the WeChat public account [Code out Offer] to receive various learning materials!

One, Springboot overview

1.1 Introduction to Springboot

SpringBoot is developed by the Pivotal team. SpringBoot is not a new technology. It just encapsulates the commonly used frameworks such as Spring, SpringMVC, data-jpa, etc., to help you hide the integration details of these frameworks and achieve agile development. Simply put, SpringBoot is a starter.

1.2 Features of Springboot

  • The SpringBoot project does not require templated configuration.
  • When integrating third-party frameworks in SpringBoot, you only need to import the corresponding starter dependency package to automatically integrate.
  • SpringBoot has only one configuration file of .properties by default, xml is not recommended, and .java files will be used to write configuration information later.
  • When the SpringBoot project is deployed, it uses the jar package method, which automatically relies on the Tomcat container internally, and provides multi-environment configuration.
  • The microservice framework SpringCloud to be learned later needs to be built on the basis of SpringBoot.

Two, quickly build Springboot

2.1 Spring official quick build

New Project
image-20200806123015729
Spring Initializer Project Setting
image-20200806132012542
Select version and required dependencies
image-20200806132155855
Fill in the project name and local storage path
image-20200806132305740

2.2 aliyun quick build (solve loading failure)

If there is a problem with Spring's official quick build loading, you can use the Spingboot provided by aliyun to quickly build.

Aliyun quickly builds Springboot
image-20200806133208551
Other steps are consistent with Spring official build

2.3 Difference between Spring and aliyun

There are some gaps between the Springboot projects that are quickly built using Spring official and aliyun. In fact, there is no essential gap in the project structure, but a series of gaps will appear in the pom.xml file!

  • Aliyun uses and maintains the 2.3 version of Spring Family Bucket, which adds some coded character sets to handle the dependencies maintained by aliyun, etc.
  • Spring officially uses and maintains the 2.3.2 version of the Spring Family Bucket, and the maintenance uses Spring official dependencies, etc.

Note: There is no essential difference between the two, we just need to use it!

2.4 Import core dependencies

This core dependency has most of Spring's web dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.5 Write Controller Direct Test

Directly visit the path to display the page and respond on the browser!

@RestController
public class TestController {
    
    
    @GetMapping("/test")
    public String test(){
    
    
        return "Hello SpringBoot!";
    }
}

Three, Springboot directory structure description

Springboot directory structure
image-20200809132750413

Four, three startup methods of Springboot

4.1 Run the main method of the startup class

Run the main method of the startup class
image-20200809133203728

4.2 Start with Maven

Start using Maven (mvn spring-boot:run)
image-20200809133408621

4.3 Start using the jar file packaged by Springboot

First of all, the Springboot packaging method is the jar method. By default, the maven package is used for packaging, and the springboot built-in will also be triggered. springboot:repackageSpringboot's built-in re-package will build a tomact on the basis of this jar file, which means that the jar file packaged with Springboot can be executed!

Package display
image-20200809133839975
DOS command line execution
java -jar jar文件路径/jar包名字

Five, Springboot entry notes

5.1 @Configuration and @Bean

  • When SSM was used for development before, bean tags were written in xml files, but SpringBoot does not recommend using xml files.
  • @Configuration annotation is equivalent to beans tag
  • @Bean annotation is equivalent to bean tag
  • id="method name|name attribute in annotation (higher priority)"
  • class="The return result of the method"

Here is an example.

@Configuration   // 代表当前类是一个配置类
public class UserConfig {
    
    
    @Bean(name = "user1")       // 构建一个实例,放到spring容器中
    public User user(){
    
    
        User user = new User();
        user.setId(1);
        user.setName("张三");
        return user;
    }
    /* 与其相同作用
    <beans ....>            @Configuration
        <bean id="user1" class="com.ziphtracks.firstspringboot.bean.User" />
    </beans>
     */
}

5.2 @SpringBootApplication

@SpringBootApplication is a composite annotation:

  • @SpringBootConfiguration is the @Configuration annotation, which means that the startup class is a configuration class.
  • @EnableAutoConfiguration helps you to achieve automatic assembly. When the SpringBoot project starts, run a SpringFactoriesLoader class, load the META-INF/spring.factories configuration class (already opened), and use the load method in SpringFactoriesLoader in a for loop. One load.
    • Benefits: No need to write a lot of integration configuration information, just follow the convention provided by SpringBoot to integrate.
    • Disadvantage: If you import a starter dependency, then you need to fill in the necessary configuration information.
    • Manually turn off the auto-assembly specified content: @SpringBootApplication(exclude = QuartzAutoConfiguration.class)
  • @ComponentScan is equivalent to <context:component-scan basePackage="package name" /> to help scan annotations.

Note: You can read the source code of the annotation below through the above

Source code
image-20200809135539749

5.3 @SpringBootTest

This annotation is used in the test startup item, the annotation used to start the test

Test startup item annotation configuration
image-20200809141258160

Six, Springboot configuration file

6.1 Configuration file specification

The configuration file of SpringBoot supports properties and yml, and even it supports json.

It is more recommended to use the yml file format:

  1. The yml file will help us manage the location of the configuration file according to line breaks and indentation

  2. yml file, more lightweight than properties

Note for yml files:

  1. Strictly follow line breaks and indentation

  2. When filling in value, be sure to follow: followed by a space

6.2 Multi-environment configuration

Add a configuration item in the application.yml file:

You can use commas (,) to activate multiple environments

As follows I created two development configurations, one for development (dev) and the other for testing (test), and then use the total configuration to activate these two configuration environments.

Multi-environment configuration
image-20200809142928925

6.3 Import external configuration file information

Like the traditional SSM method, the content in the properties/yml file is obtained through the @Value annotation.

If you need to write a large number of custom configurations in the yml file and have a unified prefix, use the following method

// Java程序
@ConfigurationProperties(prefix = "aliyun")
@Component
@Data
public class AliyunProperties {
    
    

   private String name;
   private Integer age;
}

// 配置文件
aliyun:
  name: Ziph
  age: 18

6.3 Rebuild the project

build
image-20200809143338884

6.4 Springboot hot deployment

When doing SSM projects, we will use IDEA plug-in JRebel hot deployment artifact to facilitate development. Because hot deployment is very popular with everyone, Springboot also provides a hot deployment function. as follows:

Import dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
IDEASetting related configuration
image-20200809171517524
Springboot startup item settings
image-20200809171648412

Seven, Springboot creates a web directory

The static folder stores static resources, and our dynamic resources still need to be stored in the web directory structure, so we create a web directory structure!

Import JSP and JSTL core dependencies

<!--JSP核心引擎依赖-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--JSTL-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
Create web directory structures such as webapp and WEB-INF
image-20200809172236167
Add web.xml
image-20200809172408724
image-20200809172554940
Show web directory structure
image-20200809172758250

Next article, Springboot integrates various components, please support! Thank you!

Guess you like

Origin blog.csdn.net/weixin_44170221/article/details/107897207