美文网首页
Springboot 入门认知

Springboot 入门认知

作者: 阿西Enzo | 来源:发表于2018-05-11 11:36 被阅读0次

Springboot 入门认知

@(spring boot)[spring, springboot]


[TOC]

什么是Springboot

  • 1.轻松创建独立应用程序
  • 2.提供starter 简化依赖包的配置
  • 3.开箱即用,尽可能的减少配置
  • 4.内嵌 tomcat、jetty等web容器, 可以直接点击jar启动服务,无需war部署

快速创建

使用工具

SpringInitizercli 工具,帮助我们快速选择组件构件项目

网页方式:

@网页版|center

IDEA方式:


@IDEA版|center

做了什么

便于理解,需要Maven 相关基础

  • 1.加入spring boot parent依赖,管理依赖starter的版本兼容性
  • 2.加入选择组件的starter
    感兴趣的可以深入了解一下 pom.xml 文件内的这个几个artifact:
    spring-boot-starter-parent
    spring-boot-starter-web
    mybatis-spring-boot-starter
    spring boot的github上有源码!
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

starter管理依赖

习惯优于配置 spring boot核心理念

这里我们已spring-boot-starter-web 来分析一下starter是如何进行依赖管理的
1.首先下载源码,打开项目目录, 里面啥代码都没有,只有一个pom文件

image.png

2.pom 分析

# parent: 这个我们不做深入分析啦
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starters</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>
# 来看看 这个starter 里面配置了那些web 需要的依赖包,直接调到dependences
spring-boot-starter
spring-boot-starter-json
spring-boot-starter-tomcat
hibernate-validator
spring-web
spring-webmvc
# 其中核心的,我们关注spring-web、spring-webmvc 这两个,因为这是我们使用SpringMVC需要依赖的artifact(对应的版本兼容已经给出!),此外,还提供tomcat的依赖,应该是嵌入式的tomcat 有空深入研究.所以引入该starter ,SpringMVC 相关依赖就自动加载到当前项目中了.

自动化组件配置

  • spring boot 相较传统的Spring的应用,最大的特点是去配置化. 习惯优于配置.
  • 在spring boot 中是没有spring-xxx.xml等配置文件的(虽然说你可以使用注解替代xml配置,但是部分组件的配置还是需要xml去配置组装的,但是Spring boot是完全抛弃xml)所有的配置全部由autoConfiguration 来完成,在加入starter 后,通过condition 方式自动检测,进行配置(不是没有配置,而是使用自动配置, 我理解这应该就是所谓的 习惯优于配置)
    下面我们用DispatcherServletAutoConfiguration 来分析一下SpringMVC 是如何进行自动化配置的

autoConfiguration

  • @Configuration

个人理解: 用来修饰类,该类用来进行自动配置,内部被@Bean 注解的元素都将被配置到Spring工程当中(类似 spring-xxx.xml的功能)

  • @Conditional

配合@Configuration使用, 用来判断是否对声明@Configuration的配置类进行自动配置,满足条件则该自动配置生效,启动时将自动将配置组织到spring工程当中.
当然, spring boot 提供的conditional注解有许多继承实现:

@ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean)
@ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)
@ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean)
@ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean)
@ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean)
@ConditionalOnNotWebApplication(不是web应用)
@ConditionalOnClass|center

DispatcherServletAutoConfiguration

# DispatcherServlet 在spring-mvc依赖包中,web starter引入
@ConditionalOnClass(DispatcherServlet.class)
# ServletRegistration 在tomcat 依赖保重, web starter引入
@ConditionalOnClass(ServletRegistration.class)

# 满足上述条件后 注入DispatcherServlet (还有相关配置,这里不赘述)
@Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
        public DispatcherServlet dispatcherServlet() {
            DispatcherServlet dispatcherServlet = new DispatcherServlet();
            dispatcherServlet.setDispatchOptionsRequest(
                    this.webMvcProperties.isDispatchOptionsRequest());
            dispatcherServlet.setDispatchTraceRequest(
                    this.webMvcProperties.isDispatchTraceRequest());
            dispatcherServlet.setThrowExceptionIfNoHandlerFound(
                    this.webMvcProperties.isThrowExceptionIfNoHandlerFound());
            return dispatcherServlet;
        }

全局配置文件

application.yml

IDEA 默认创建的是application.properties, 这里推荐使用yml(个人感觉可读性更好,大趋势也是在推yml)
# 配置服务端口
server:
  port: 8080

web.xml 去哪了

传统Web应用,我们通过Web.xml 来配置web 容器中的servlet、filter、listener等信息,但是Springboot 是没有web.xml文件的。熟悉SpringMVC的同学,可能困惑啦,因为SpringMVC 的DispatchServlet 核心servlet便是在web.xml 中注册的。
了解上文的starte 与 autoConfiguration DispatchServlet 应该可以理解了)
自定义的servlet 、filter、listener如何配置了,答案是:

@Configuration
 + @Bean
 +:FilterRegistrationBean\ServletRegistrationBean\ServletListenerRegistrationBean

相关文章

网友评论

      本文标题:Springboot 入门认知

      本文链接:https://www.haomeiwen.com/subject/nkhfdftx.html