课程介绍
项目用到的技术与复习
springboot与springmvc的对比
1. 与springmvc的对比: Spring Boot引入自动配置的概念,让项目设置变得很容易。Spring Boot本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速、敏捷地开发新一代基于Spring框架的应用程序。
2. 设计目的: 用来简化 Spring 应用的初始搭建以及开发过程。
从最根本上来讲,Spring Boot 就是一些库的集合,它能够被任意项目所使用。它使用 “习惯优于配置” (项目中存在大量的配置,此外还内置一个习惯性的配置)的理念让你的项目快速运行起来。spring boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 maven 整合了所有的 jar 包,spring boot 整合了所有的框架,总结一下及几点:
(1)为所有 Spring 开发提供一个更快更广泛的入门体验。
(2)零配置。无冗余代码生成和XML 强制配置,遵循“约定大于配置” 。
(3)集成了大量常用的第三方库的配置, Spring Boot 应用为这些第三方库提供了几乎可以零配置的开箱即用的能力。
(4)提供一系列大型项目常用的非功能性特征,如嵌入服务器等。
3. SpringBoot的特点:
为基于Spring的开发提供更快的入门体验*
开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求
提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、外部配置等*
SpringBoot不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式
4. 与Struts2的对比:Struts2漏洞太多,影响巨大,这也是 Struts2 被淘汰的主要原因。各种补丁包替换麻烦,成本高。
5. springboot的启动依赖有starter,该starter包含了所有跟该环境有关的依赖,使得开发人员不必要去进行版本控制,加快开发进度。
6. 自动配置原理:通过启动类注解@SpringBootApplication
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class);
}
}
此注解是复合注解包含三个重要注解:
@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 {
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
@AliasFor(annotation = EnableAutoConfiguration.class)
Class<?>[] exclude() default {};
... ... ...
}
@ComponentScan
(包扫描)
component是组件,scan是扫描,所以这个注解的含义就是用来扫描组件的,componentScan 扫描当前包及其子包下被 @Component
,@Controller,@Service,@Repository
注解标记的类并纳入到spring容器中进行管理,所以这个注解会自动注入所有在主程序所在包下的组件。默认把当前启动类所在的包作为扫描包的起点,例如咱们的项目,扫描 com.maweiqi 包
以前在ssm项目中我们需要去配置我们的包扫描
<context:component-scan base-package="com.xxx"></context:component-scan>
@SpringBootConfiguration
: 表示当前类具有配置类的作用
自动配置:根据当前引入的依赖包,猜测需要创建的工程类型,以及工程中有可能创建的对象,根据猜测自动创建工程所需的相关实例bean
程序启动,会自动加载扫描所有 classpath:/META-INF/spring.factories文件 ,并且创建对应实例
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
... ... ...
}
其中,@Import(AutoConfigurationImportSelector.class)
导入了 AutoConfigurationImportSelector
类
加载元数据
其中,SpringFactoriesLoader.loadFactoryNames
方法的作用就是从META-INF/spring.factories
文件中读取指定类对应的类名称列表
spring.factories
文件中有关自动配置的配置信息如下:摘抄重点,springboot 启动之后,会自动加载 dispatcherServlet
上面配置文件存在大量的以 Configuration 为结尾的类名称,这些类就是存有自动配置信息的类,而SpringApplication 在获取这些类名后再加载
我们以 DispatcherServletAutoConfiguration
为例来分析:
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(DispatcherServlet.class)
@AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class)
public class DispatcherServletAutoConfiguration {
/*
* The bean name for a DispatcherServlet that will be mapped to the root URL "/"
*/
public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet";
- 基本上没有xml @Configuration @SpringBootConfiguration,主要使用配置类进行对应的组件添加和定制化
- 约定大于配置