Spring Boot 原理解析—预备篇

Spring Boot是为了简化Spring开发而对Spring的进一步封装,是对Spring的增强。要弄清楚Spring Boot,首先需要弄清楚Spring Boot与Spring的使用,到底简化了那些东西,Spring Boot对Spring封装时使用了Spring的那些东西。我们分别以原生Spring MVC和使用Spring Boot的Spring MVC为例,查看Spring Boot到底怎么简化Spring开发的。

依赖包的引入:

在使用原生Spring MVC开发时,我们需要逐个引入spring-core,spring-webmvc,spring-webmvc,spring-context等一系列Spring提供的jar包,如果再涉及到数据库,redis等还需要逐个引入与数据库和redis相关的jar包。使用Spring Boot之后,我们只需要引入spring-boot-starter-web就可以创建一个最简单的web应用。如果涉及到数据库或者其他第三方插件,只需要引入对应的starter包即可,比如:spring-boot-starter-data-jpa,spring-boot-starter-data-redis等,更多starter包可以查看Spring 官网。

配置文件配置:

不使用Spring Boot开发时,我们需要一系列繁复的配置,首先是web.xml配置。然后是Spring.xml配置。我们需要在web.xml中配置DispatcherServlet,CharacterEncodingFilter以及其他过滤器,配置spring.xml的位置contextConfigLocation。在spring.xml中需要配置大量的bean。

<bean id="viewResolverCommon"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/views/" />
	<property name="suffix" value=".jsp" />
<!--			可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑-->
	<property name="viewClass">
	<value>org.springframework.web.servlet.view.InternalResourceView
			</value>
	</property>
	<property name="order" value="1" />
	</bean>

	<!-- 视图解析器 -->
	<!-- 配置freeMarker视图解析器 -->
<bean id="viewResolverFtl"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
	<property name="viewClass" value="com.sojson.core.freemarker.extend.FreeMarkerViewExtend" />
		<!-- 把Freemarker 扩展一下,把相关属性加入进去。。。 -->
	<property name="contentType" value="text/html; charset=utf-8" />
	<property name="cache" value="true" />
	<property name="suffix" value=".ftl" />
	<property name="order" value="0" />
</bean>
<bean id="viewResolver"
	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="order" value="2"></property>
	<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
	<property name="prefix" value="/WEB-INF/views/" />
	<property name="suffix" value=".jsp"></property>
</bean>

如下为spring.xml配置简单案例,这只是一部分配置,如果涉及到数据库,缓存,权限控制,需要配置的更多。而Spring Boot,则是不需要这么多bean的配置,甚至不需要web.xml配置。在引入对应的starter包之后,配置相应的Properties或者yml配置文件即可。Spring Boot会根据引入starter个Properties配置自动注入Bean。如下是Spring Boot使用mybatis-plus的配置。

server:
   port: 9009
spring:
   application:
      name: mybatisplus-starter
   datasource:
      filters: stat,wall,slf4j
      initialSize: 5
      maxActive: 20
      maxPoolPreparedStatementPerConnectionSize: 20
      maxWait: 60000
      minEvictableIdleTimeMillis: 300000
      minIdle: 5
      poolPreparedStatements: true
      testOnBorrow: false
      testOnReturn: false
      testWhileIdle: true
      timeBetweenEvictionRunsMillis: 60000
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:mysql://localhost:3306/mybatisplus?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
      username: root
      password: 123456
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;password=${spring.datasource.password}
      driverClassName: com.mysql.jdbc.Driver
      validationQuery: SELECT 1 FROM DUAL
mybatis-plus:
   configuration:
      cache-enabled: false
      map-underscore-to-camel-case: true
      log-prefix: cn.org.microservice.mybatisplus.starter.mapper
   global-config:
      db-column-underline: true
      field-strategy: 0
      id-type: 0
      refresh-mapper: true
   mapper-locations: classpath:/mapper/*mapper.xml
   typeAliasesPackage: cn.org.microservice.mybatisplus.starter
   

程序的运行方式:

在非Spring Boot的MVC程序中,我们需要先将程序打包成war包,然后部署到WEB服务中。而Spring Boot运行应用程序更为简单,因为Spring Boot可以内嵌Tomcat或者jetty服务器,我们只需要运行@SpringBootApplication注解的包含main方法的类,即可以运行一个web程序。

其实Spring Boot做的主要是Bean的自动配置和较少依赖的引入,而核心是Bean的自动配置。也是后面我们讲的重点,在介绍Bean的自动配置之前,我们需要了解Spring注解驱动开发的知识:比如@ComponentScan,@Bean,@Conditional,@Primary,@PropertySource,@PropertySources,@Profile,@EnableXXX,xxxAware,@Configuration,@Import,@ImportResource以及AOP注解等的使用,关键是熟悉Spring的原理以及设计模式。Spring Boot在自动装配中很多都是用这写注解或者增强这些注解来完成的。如果掌握了这些注解,你就也可以编写自己的starter包。后续源码解析中,用到Spring原理的内容会写到Spring专栏中,Spring Boot内部写到Spring Boot专栏

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值