spring boot学习

Maven安装

Maven是一款服务于Java平台的自动化构建工具.

下载

下载地址.
我是把解压后放在了这个目录C:\Program Files (x86)\Maven\apache-maven-3.6.1
添加系统环境变量,MAVEN_HOME:C:\Program Files (x86)\Maven\apache-maven-3.6.1,就是maven的地址.然后再编辑Path变量,添加一条:%MAVEN_HOME%\bin.
重新打开cmd,输入mvn -v就可以看到版本信息,说明环境变量配好了.

配置

给maven的settings.xml配置文件的profiles标签添加:

1
2
3
4
5
6
7
8
9
10
11
12
<profile>
<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>
</profile>

settings标签里加入本地仓库标签<localRepository>C:\Program Files (x86)\Maven\apache-maven-3.6.1\repository</localRepository>

IDEA设置

IDEA找到settings->Build,Execution,Deployment->Build Tools->Maven,然后把Maven Home Directory设置成Maven目录,User Settings file改到Maven目录下conf->settings.xml,Local repository改到Maven目录下的repository文件夹,

Spring Boot HelloWorld

浏览器发送hello请求,服务器接受请求并处理,响应Hello World字符串.

创建maven工程

会自动选择编译器,next.

1
2
3
GroupId:com.chenxiyuan
ArtifactId:spring-boot-helloworld
Version:1.0-SNAPSHOT

然后next,进入到项目界面,右下角会弹出一个框,选择Enable Auto-Import启用自动导入,以后每写一个依赖,都会自动导入jar包.

导入springboot依赖

填到pom.xml里:

1
2
3
4
5
6
7
8
9
10
11
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

然后会自动导入相关的jar包.

主程序

main->java文件夹下新建一个类名com.chenxiyuan.HelloWorldMainApplication.

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.chenxiyuan;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

编写相关的Controller,Service

com.chenxiyuan包新建一个类controller.HelloController:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.chenxiyuan.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {


@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}

}

运行主程序

访问127.0.0.1:8080/hello,成功输出Hello World!.

简化部署

也把以下插件导入到pom.xml里:

1
2
3
4
5
6
7
8
9
<!-- 这个插件,可以将应用打包成一个可执行的jar包;-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Markdown
打包好以后,就在项目的target文件夹下生成了一个jar包spring-boot-helloworld-1.0-SNAPSHOT.jar.
我把它复制到桌面,打开cmd,执行(记得把之前调试关掉):

1
java -jar spring-boot-helloworld-1.0-SNAPSHOT.jar

成功运行起来了,可以访问http://127.0.0.1:8080/hello查看效果.

Hello World探究

POM文件

打开pom.xml

父项目

1
2
3
4
5
6
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

导入spring-boot-starter的父项目,做依赖管理.ctrl+b跟踪进去,发现它也有一个父项目:

1
2
3
4
5
6
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>

它来真正管理Spring Boot应用里面的所有依赖版本.继续跟进,可以发现<properties>标签里有所有依赖.

启动器

导入的依赖:

1
2
3
4
5
6
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

Spring Boot将所有的功能场景都抽取出来,做成一个个的starter(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来.要用什么功能就导入什么场景的启动器.
spring-boot-starter-web启动器,帮我们导入了web模块正常运行所依赖的组件.ctrl+b跟进去可以看到:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.0.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.0.0.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.0.0.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.7.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.4.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>

主程序类,主入口类

1
2
3
4
5
6
7
@SpringBootApplication//标注一个主程序类,说明这是一个Spring Boot应用
public class HelloWorldMainApplication {
public static void main(String[] args){
// Spring应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}

@SpringBootApplication:Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用,跟踪进去:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {

@SpringBootConfiguration

@SpringBootConfiguration:Spring Boot的配置类,跟踪进去:

1
2
3
4
5
6
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}

@Configuration:配置类,继续跟踪进去,可以看到容器的底层组件@Component:

1
2
3
4
5
6
7
8
9
10
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(
annotation = Component.class
)
String value() default "";
}

@EnableAutoConfiguration

@EnableAutoConfiguration:开启自动配置功能:

1
2
3
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {

@AutoConfigurationPackage:自动配置包,跟进:

1
2
3
4
5
6
7
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
}

@Import({Registrar.class}):​Spring的底层注解@Import,将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器.

使用Spring Initializer快速创建Spring Boot项目

IDEA里new project时候选择Spring Initializr可以快速创建项目.
Markdown
Markdown
resources文件夹中目录结构:

  • static:保存所有的静态资源:js,css,images.
  • templates:保存所有的模板页面(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面).可以使用模板引擎(freemarker、thymeleaf).
  • application.properties:Spring Boot应用的配置文件,可以修改一些默认设置