53. First acquainted with SpringBoot

table of Contents

One, the benefits of SpringBoot

Two, environment configuration

Three, HelloWorld

Fourth, analyze HelloWorld (this chapter involves the source code, you can skip)

1.spring-boot-starter-parent

2.spring-boot-starter-web

3. Startup

Five, use Spring Initializer to quickly create a SpringBoot project


One, the benefits of SpringBoot

SpringBoot is a framework to simplify application development:

1. Quickly create projects and integrate with mainstream frameworks

2. No need to make a war package

3. Starters automatic dependency and version control

4. A large number of automatic configuration, no xml

5. Natural integration with cloud computing

 

Two, environment configuration

1.maven

The settings in the <profiles> tag in the maven setting are as follows:

<?xml version="1.0" encoding="UTF-8"?>


<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 
  <localRepository>D:/mavenSource/xupeng_springBoot/resource</localRepository>
  
  <pluginGroups>
  </pluginGroups>

 
  <proxies>
  </proxies>

 
  <servers>
  </servers>

  <mirrors>
	<mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
	<mirror>
          <id>oschina-repo</id>
          <name>openSourceChina</name>
          <mirrorOf>central</mirrorOf>
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      </mirror>
	
  </mirrors>

  <profiles>
	<id>jdk-1.8</id>
	<activation>
		<activeByDefault>true</activeByDefault>
		<jdk>1.8</jdk>
	</activation>
	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
	</properties>
  </profiles>
</settings>

2.idea

Enter "maven" in the upper left corner, select the corresponding maven and configuration file

 

Three, HelloWorld

Realization function: the browser sends hello, the server receives the request and processes it, and responds to the HelloWorld string.

1. Create a maven project

 

2. Import spring boot related dependencies

pom.xml

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

3. Write the main program class

HelloWorldMainApplication:

/*
* @SpringBootApplication:标注主程序类,说明这是一个springboot应用
* */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

4. Write related controllers

HelloController :

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "hello xupeng";
    }
}

5. Run

Click the green button in front of the start class

6. Deployment

Add the following configuration in pom.xml:

    <!--这个插件可以将应用打包成一个可执行的jar包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

Click clean, install, package in turn. You will see a jar package is generated. Copy this jar package to the desktop.

Right-click to open the jar package and copy its path. Then enter cmd to run:

java -jar

Fourth, analyze HelloWorld (this chapter involves the source code, you can skip)

If you are done up there, is there only one word: cool? So let's analyze and analyze how springboot works:

1.spring-boot-starter-parent

We click into <parent>, then click into <parent>, and finally come to spring-boot-dependencies. This is the version arbitration center. It helps us import many jar packages and corresponding versions, so there are many jars behind. We import the package without dependency.

Of course, if the version arbitration center does not have a dependency, we need to manually add the version

2.spring-boot-starter-web

The father of spring-boot-starter-web is spring-boot-starter

spring-boot-starter: The spring-boot scene starter helps us import the components that we depend on for normal operation.

spring-boot-starter-web: spring-boot-starter-web helps us import the components that the web module depends on for normal operation.

(Looking at these dependencies, I did think of the days when I was learning mvc...)

Springboot extracts all the scenes into starters (starters), only need to introduce these starters in the project, all the dependencies of the relevant scenes will be imported in

3. Startup

@SpringBootApplication: Annotate the main configuration class, SpringBoot runs the main method of this class to start the SpringBoot application

 

 

@SpringBootApplication is composed of a series of annotations:

(1)@SpringBootConfiguration:表示这是一个SpringBoot的配置类
  • @Configuration: This annotation indicates that the annotated class is a configuration class. One of the configuration classes corresponds to a previous configuration file.
  • The @Configuration configuration class is also a component @Component in the container, that is to say, it will be used by the spring container IOC, DI
(2)@EnableAutoConfiguration:开启自动配置功能

The automatic configuration function saves us the work of manually writing configuration injection function components.

  • @AutoConfigurationPackage:自动配置包。我们点进去这个注解,看到有一个@Import({Registrar.class})。进入到Registrar里,看到registerBeanDefinitions方法,我们以断点的方式打开,然后计算getPackageName,会发现得到的是配置类的路径名。也就是说,这个注解将主配置类(@SpringBootApplication)的所在包及厦门所有子包的所有组件扫描进spring容器
  •  
  • There is also an EnableAutoConfigurationImportSelector class under @EnableAutoConfiguration : all the components that need to be imported are returned as full class names, these components are added to the container, and a lot of automatic configuration classes are imported into the container . Among them, there is a SpringFactoriesLoader.loadFactoryNames method under getCandidateConfigurations in selectImports, which specifies the source of the components that need to be loaded: META-INF/spring.factories. We then go to spring.factories in spring-boot-autoconfigure.jar, where we specify the components we need to import.
     

 

Five, use Spring Initializer to quickly create a SpringBoot project

1. Select Spring Initializr to create a spring project

This is created

2. Create the controller

HelloController :

/*@RestController = @ResponseBody + @Controller*/
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        return "hello xupeng";
    }
}

3. Table of Contents

static: save all static resources: js, css image

templates: save all template pages (springboot does not support jsp), you can use template engines (freemarker, thymeleaf)

application.properties: configuration file of springboot application, some default properties can be modified

Guess you like

Origin blog.csdn.net/qq_40594696/article/details/111222222