[Spring框架学习之路篇] SpringBoot 快速开发启动器

SpringBoot 是 Spring 快速开发的脚手架,通过约定大于配置的方式,快速构建和启动 spring 项目。

由于按原来的方法创建一个 spring 工程需要编写大量的 xml 配置文件,并且存在复杂的依赖管理,不利于编程人员的开发,于是 SpringBoot 就诞生了。

SpringBoot 的特点

  • 快速开发 spring 应用的框架
  • 内嵌 tomcal 和 jetty 容器,不需要单独安装容器,jar 包直接发布一个 web 应用
  • 简化 maven 配置,以 parent 方式,一站式引入需要的各种依赖而不产生版本冲突
  • 基于注解的零配置思想
  • 可以和各种流行的框架如 spring web mvc、mybatis、spring cloud 无缝整合

SpringBoot 开发流程

配置pom.xml文件

引入父工程坐标,管理各种常用依赖

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>版本号</version>
</parent>

添加web启动器,自动配置web服务

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
</dependencies>

管理jdk版本

<properties>
	<java.version>jdk版本</java.version>
</properties>
创建启动类

通过启动类的 main 函数启动 web 项目
可以通过 IDEA 编译器的 JBLSpringBootAppGen 插件快速生成 Application 启动类和 application.yml / application.xml 配置文件

// 启动类 Application
@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}
后面就可以照常编写 Controller、Service、Dao、实体类等完成整个 Web 项目的开发

SpringBoot 属性注入

SpringBoot 采用全注解配置的方式完成属性注入。

  • @Configuration 声明一个类作为配置类,代替xml文件
  • @Bean 声明在方法上,将方法的返回值加入Bean容器,代替xml文件中的<bean>标签
  • @Value 属性注入
  • @PropertySource 指定外部属性文件

另外,原本可以用 Spring 注解配置的地方均可以用注解配置,如 @Component@Service@Autowired@Resource等。

SpringBoot 自动配置原理

每一个项目的启动类都会有一个 @SpringBootApplication的注解,这是自动配置的关键。

@SpringBootApplication
public class MpApplication {
	public static void main(String[] args) {
		SpringApplication.run(MpApplication.class, args);
	}
}

@SpringBootApplication详细如下:

// @SpringBootApplication 注解源码
@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 {

其中包含以下3个重要注解:

  • @SpringBootConfiguration
  • @EnableAutoConfiguration
  • @ComponentScan
@SpringBootConfiguration

@SpringBootConfiguration源码如下:

// @SpringBootConfiguration 注解源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}

其中又包含了一个@Configuration,说明这个 Application 类也作为配置类对工程进行配置,即被@Configuration注解修饰的 Application 类也作为一个IOC容器,支持JavaConfig的方式来进行配置;

@EnableAutoConfiguration

这个注解可以使 SpringBoot 基于添加的依赖,配置相关的依赖。例如,pom.xml 中引入了 spring-boot-starter-web,那么启动器就会认为本项目是一个 Web 项目,自动向项目中添加 tomcat、SpringMVC 等依赖,帮助完成 web 和 SpringMVC 的默认配置。
这个注解里面包含两个比较重要的注解@AutoConfigurationPackage和@Import。

  1. @AutoConfigurationPackage 和 @ComponentScan 一样,也是将主配置类所在的包及其子包里面的组件扫描到 IOC 容器中,但是区别是 @AutoConfigurationPackage 扫描 @Enitity、@MapperScan 等第三方依赖的注解,@ComponentScan 只扫描 @Controller/@Service/@Component/@Repository 这些常见注解。所以这两个注解扫描的对象是不一样的。
  2. @Import(AutoConfigurationImportSelector.class) 是自动装配的核心注解, AutoConfigurationImportSelector.class 中有个 selectImports 方法。
    详细介绍在以下链接。
    原文链接:SpringBoot自动装配原理.
@ComponentScan

这个注解的作用类似于<context:component-scan>标签,通过 basePackageClasses 或者 basePackages 属性来指定要扫描的包,将 @Controller / @Service / @Component / @Repository 等注解加载到 IOC 容器中。如果没有指定这些属性,SpringBoot就会从声明这个注解的类所在的包开始,扫描包及其子包来注入@Component注解及其子注解的属性。

注意:这个@SpringBootConfiguration注解只会出现在 main 函数所在的启动类中,为了覆盖项目中所有需要注入属性的类,启动类一般会放在总的包目录中。

SpringBoot 默认配置

由于@EnableAutoConfiguration的存在,Spring 会根据引入的 spring-boot-autoconfigure 依赖中的自动配置类,安排很多模块的默认配置,以下以 SpringMVC 为例。
WebMVCAutoConfiguration源码如下:

// WebMVCAutoConfiguration 自动配置类源码
@Configuration
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {

其中包含以下4个注解:

  • @Configuration 声明这个类是一个配置类。
  • @ConditionalOnWebApplication(type =Type.SERVLET) 声明这个类的配置类型是 Servlet 。
  • @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class}) 检测是否添加了大括号指定的类,符合条件才进行自动配置,其中 Servlet 会伴随 tomcat 依赖自然引入,另外两个类则是有 SpringMVC 引入的。
  • @ConditionalOnMissingBean({WebMvcConfigurationSupport.class}) 检测是否添加了大括号指定的类,若符合条件,则不进行默认配置,因为这是要进行自定义配置才会引入的类。

这个 WebMVCAutoConfiguration 类配置了很多关于 SpringMVC 中处理请求的对象,如视图解析器、处理器适配器等。

只要不进行自定义的配置,且引入了条件要求的相关依赖后,SpringBoot 都会按照默认的自动配置类来进行相应的配置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值