这是我在观看雷丰阳老师讲述的
SpringBoot_权威教程_spring boot_springboot核心篇+springboot整合篇(https://www.bilibili.com/video/BV1Et411Y7tQ?from=search&seid=16423369391514193863)
视频教程时做的课堂笔记。
一是能够对自己起到一个巩固学习的作用,二是希望能对各位一起学习的小伙伴有一些实质性的帮助。
一起加油吧!奥里给!
SpringBoot入门
介绍
请参考Spring官网:Spring Boot
优点
1、快速创建独立运行的Spring项目以及与主流框架集成
2、使用嵌入式的Servlet容器,应用无需打成WAR包
3、starters自动依赖与版本控制
4、大量的自动配置,简化开发,也可修改默认值
5、无需配置XML,无代码生成,开箱即用
6、准生产环境的运行时应用监控
7、与云计算的天然集成
微服务
2014,martin fowler
微服务:架构风格(服务微化)
微服务:每一个功能元素最终都是一个可独立替换和独立升级的软件单元;
一个应用应该是一组小型服务;可以通过HTTP的方式进行互通;
想了解更多请参考,
hello world
环境准备
- jdk1.7及以上
- Maven3.3.9及以上
- IDEA或Eclipse
jdk和Maven的版本
Maven配置
1、告诉Maven当前的jdk版本为1.8
操作:修改Maven中的settings.xml配置文件
在profiles标签中添加profile标签
<!-- 配置jdk1.8 -->
<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>
<maven.compiler.encoding>utf-8</maven.compiler.encoding>
</properties>
</profile>
2、配置Maven阿里云镜像
操作:修改Maven中的settings.xml配置文件
在mirrors标签中添加mirror标签
<!-- 配置阿里云镜像 -->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
IDEA配置Maven
位置:File -> Settings -> Maven
开始
创建SpringBoot项目
官网教程:https://spring.io/quickstart
创建完成后的目录结构
相关依赖
<?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 https://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.4.0</version>
<relativePath/>
</parent>
<groupId>com.xianxian86</groupId>
<artifactId>springboot-01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--该插件可以将项目打包成一个可执行的JAR包-->
<!--将这个应用打包成一个可执行的JAR以后,可以直接使用java -jar xxx 命令执行-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
SpringBoot主程序类
package com.xianxian86;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//@SpringBootApplication:标注该类为SpringBoot的启动类
@SpringBootApplication
public class Springboot01Application {
//启动Spring应用
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}
Controller控制器
package com.xianxian86.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@ResponseBody
@RequestMapping("/test")
public String test(){
return "hello world";
}
}
运行SpringBoot主程序测试
测试成功
hello world探究
pom文件
父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/>
</parent>
<!--好处: 使用SpringBoot,导入依赖不需要编写版本号-->
<!--由它来管理SpringBoot应用中的所有依赖版本-->
<!--可以将它称之为SpringBoot的版本仲裁中心-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.0</version>
</parent>
<!--注意: 没有在dependencies里面管理的依赖(properties标签中),需要手动编写声明版本号-->
<!--<properties>
<activemq.version>5.16.0</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.83</appengine-sdk.version>
<artemis.version>2.15.0</artemis.version>
<aspectj.version>1.9.6</aspectj.version>
<assertj.version>3.18.1</assertj.version>
.....
</properties>
-->
启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
例: spring-boot-starter-web
spring-boot-starter-test
spring-boot-starter: spring-boot场景启动器;导入对应模块(web、test)正常运行所需要依赖的组件
SpringBoot将所有的功能场景都抽取出来,做成不同的starters启动器,用户只需要在项目中引入这些不同的starters启动器,那么所有相关场景的依赖都会导入进来。
简单的说,就是需要什么功能场景就导入什么场景的启动器
主配置类
@SpringBootApplication
public class Springboot01Application {
public static void main(String[] args) {
//启动SpringBoot应用
SpringApplication.run(Springboot01Application.class, args);
}
}
@SpringBootApplication:标注在某个类上,说明这个类是SpringBoot应用,也是SpringBoot的启动类(入口类)
要想启动SpringBoot应用,就只需要运行这个类的main方法
@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
字面意思为:SpringBoot的配置类
将该注解标注在某个类上,说明这个类为SpringBoot的配置类
@Configuration
注解表明当前类是一个配置类
在Spring当中经常使用该注解来表明一个类是配置类
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
@AliasFor(
annotation = Configuration.class
)
boolean proxyBeanMethods() default true;
}
@Component
配置类也是容器当中的一个组件
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(
annotation = Component.class
)
String value() default "";
boolean proxyBeanMethods() default true;
}
配置类相当于配置文件
- @EnableAutoConfiguration
字面意思为:开启自动配置
使用了该注解,才能让SpringBoot的自动配置生效
@AutoConfigurationPackage
字面意思为:自动配置包
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
@Import({Registrar.class})
解释1:快速给容器中导入一个组件,容器中自动注册这个组件,id是默认全类名
解释2:Spring中经常使用的注解,通常用来表示给容器中导入(@Import({类名.class}))一个组件
该注解导入的组件为Registrar.class
标注在EnableAutoConfiguration上的作用:将主配置类(被@SpringBootApplication所标注的类)所在包及下面所有子包里面的所有组件扫描到Spring容器当中
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
}
//获取到了被@SpringBootApplication所标注的类所在包及下面所有子包里面的所有类名
new AutoConfigurationPackages.PackageImports(metadata)).getPackageNames()
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
Registrar() {
}
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
AutoConfigurationPackages.register(registry, (String[])(new AutoConfigurationPackages.PackageImports(metadata)).getPackageNames().toArray(new String[0]));
}
public Set<Object> determineImports(AnnotationMetadata metadata) {
return Collections.singleton(new AutoConfigurationPackages.PackageImports(metadata));
}
}
@Import({AutoConfigurationImportSelector.class})
AutoConfigurationImportSelector.class:导入哪些组件的选择器
将所有需要导入的组件以全类名的方式返回 ; 这些组件就会被添加到容器中
通俗点说,就是它会在容器当中导入非常多的自动配置类(xxxAutoConfiguration),导入这个场景所需要的所有组件,并配置好这些组件
总结:有了自动配置类,就免去了我们以前需要手动编写配置注入功能组件等的工作。SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮助我们进行自动配置的工作
J2EE的整体整合解决方案和自动配置都在Maven仓库\org\springframework\boot\spring-boot-test-autoconfigure\2.4.0\spring-boot-test-autoconfigure-2.4.0.jar!\org\springframework\boot\test\autoconfigure
使用Spring Initializr 快速创建SpringBoot项目
IDE都支持使用Spring的项目创建向导快速,快速创建一个SpringBoot项目
操作步骤
看图说明
注意:因为我是在SpringBoot-Study(文件夹)下创建的,所以左上角才会显示New Module(模块)。
一般情况下会显示New Project
创建成功
目录结构:resources文件夹中的目录结构
static:保存所有的静态资源,如js、css、images
templates:保存所有的模板页面,因为SpringBoot默认jar包使用的是嵌入式的Tomcat,默认不支持JSP页面,所以提供了模板引擎(freemarker、thymeleaf)来替代JSP
application.properties:SpringBoot应用的配置文件,可以在这里修改一些默认的设置,如默认端口号等
创建结束后,可能会出现的问题
问题:Java类文件的左下角出现红色的J标识
解决办法
选中module中的pom.xml,右键,然后点击 + add as maven project
后续文章:第四篇、第三篇、第二篇