Java学习(第一阶段模块四)2020/5/17-2020/5/24

Java学习(第一阶段模块四)

  • 任务一:SpringBoot基础回顾
    • 1. 约定优于配置
    • 2. SpringBoot解决Spring问题
    • 3. 单元测试与热部署
  • 任务二:SpringBoot原理深入及源码剖析
    • 1. 依赖管理
    • 2. 自动配置
  • 任务三:Thymeleaf

任务一:SpringBoot基础回顾

顺利完成模块三的学习,正式进入模块四。

1. 约定优于配置

约定优于配置(Conversion over configuration),又称按约定编程,是一种软件设计范式。
约定优于配置简单来理解,就是遵循约定。

2. SpringBoot解决Spring问题

  1. 起步依赖:将具备某种功能的坐标打包到一起,并提供一些默认的功能。
  2. 自动配置:会自动将一些配置类的bean注册进ioc容器中,可以使用@autowired以及@resource等注解来使用。
  3. springboot:简单,快速,方便的搭建项目。对主流的开发框架无配置集成,极大的提高了开发,部署效率。

3. 单元测试与热部署

<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
@RunWith(SpringRunner.class) // 测试启动器,并加载SpringBoot测试注解
@SpringBootTest //标记为SpringBoot单元测试类,并加载项目的ApplicationContext上下文环境
class SpringbootDemoApplicationTests {
@Autowired
private DemoController demoController;
// 自动创建的单元测试方法实例
@Test
void contextLoads() {
String demo = demoController.demo();
System.out.println(demo);
}
}

<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>

Java学习(第一阶段模块四)2020/5/17-2020/5/24_第1张图片
Java学习(第一阶段模块四)2020/5/17-2020/5/24_第2张图片
Ctrl+Shift+Alt+/打开maintenance,选中并打开Registry页面。

任务二:SpringBoot原理深入及源码剖析

1. 依赖管理

项目pom.xml文件有两个核心依赖,分别是spring-boot-starter-parent和spring-boot-starter-web。


<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.2.RELEASEversion>
<relativePath/> 
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>2.2.2.RELEASEversion>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jsonartifactId>
<version>2.2.2.RELEASEversion>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
<version>2.2.2.RELEASEversion>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-validationartifactId>
<version>2.2.2.RELEASEversion>
<scope>compilescope>
<exclusions>
<exclusion>
<artifactId>tomcat-embed-elartifactId>
<groupId>org.apache.tomcat.embedgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>5.2.2.RELEASEversion>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.2.RELEASEversion>
<scope>compilescope>
dependency>
dependencies>

2. 自动配置

@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
@Target({ElementType.TYPE}) //注解的适用范围,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配置类。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration //配置IOC容器
public @interface SpringBootConfiguration {
}

@EnableAutoConfiguration注解表示开启自动配置功能,该注解是SpringBoot框架最重要的注解,也是实现自动化配置的注解。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage//自动配置包
@Import({AutoConfigurationImportSelector.class})//自动配置类扫描导入
public @interface EnableAutoConfiguration{
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() defalult{};
String[] excludeName() default {};
}

@AutoConfigurationPackage

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class}) // 导入Registrar中注册的组件
public @interface AutoConfigurationPackage {
}

@Import({AutoConfigurationImportSelector.class})
查找classpath上所有jar包中的META-INF/spring.factories进行加载,实现将配置类信息交给SpringFactory加载器进行一系列的容器创建过程。
@ComponentScan注解具体扫描的包的根路径由SpringBoot项目主程序自动启动类所在的包位置决定,在扫描过程中由前面介绍的@AutoConfigurationPackage注解进行解析,从而得到SpringBoot项目主程序启动类所在包的具体位置。

任务三:Thymeleaf

Java学习(第一阶段模块四)2020/5/17-2020/5/24_第3张图片
Java学习(第一阶段模块四)2020/5/17-2020/5/24_第4张图片
Java学习(第一阶段模块四)2020/5/17-2020/5/24_第5张图片

你可能感兴趣的:(Java)