下一章就进入他们的详细源码结构中看看他们的本质了,期待ing。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.2version>
dependency>
在我们的Spring Boot项目种的POM文件中总会看到这两种依赖:
spring-boot-starter-xxx 和 xxx-spring-boot-starter。
这就是spring boot的四大组件之一的starter。
a、spring-boot-starter-thymeleaf
两种starter的区别就是 >>
官方提供的starter是这样的:spring-boot-starter-xxx
非官方的starter是这样的:xxx-spring-boot-starter
其中xxx就是我们想要依赖的组件或者jar包。上例就是我们spring boot用来引入thymeleaf引擎和mybatis框架所配置的依赖。引入之后通过简单的约定配置就可以正常使用。比如:
Thymeleaf引擎约定配置:
##前端引擎配置
spring:
thymeleaf:
enabled: true
servlet:
content-type: text/html
mode: HTML
## 页面前缀
prefix: classpath:/templates/
## 后缀
suffix: .html
Mybatis约定配置:
mybatis:
mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.hi.ld.vo.system # 注意:对应实体类的路径
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
下面让我们来看看以前怎么配置thymeleaf。
废话不多说,直接上代码:
a. 添加对应依赖:
<dependency>
<groupId>org.thymeleafgroupId>
<artifactId>thymeleaf-spring5artifactId>
<version>3.0.11.RELEASEversion>
dependency>
<dependency>
<groupId>org.thymeleaf.extrasgroupId>
<artifactId>thymeleaf-extras-java8timeartifactId>
<version>3.0.4.RELEASEversion>
dependency>
b. bean配置
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
bean>
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
bean>
a. 添加对应依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
dependency>
b. bean配置
下面的第3, 4步骤就是Mybatis相关配置。第一步是引入资源配置。第二步是配置数据源
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<property name="autoCommitOnClose" value="false" />
<property name="checkoutTimeout" value="10000" />
<property name="acquireRetryAttempts" value="2" />
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="typeAliasesPackage" value="com.soecode.lyf.entity" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.soecode.lyf.dao" />
bean>
beans>
a、Starter 帮我们封装好了所有需要的依赖,避免我们自己添加导致的一些Jar包冲突
或者缺少包
的情况;
b、Starter帮我们自动注入了需要的Bean实例到Spring 容器中,不需要我们手动配置(这个可以说是starter干的,实际上并不是,这里埋个坑,下面解答)
;
所以:
starter包的内容就是pom文件,就是一个依赖传递包
。
autoconfigure在我们的开发中并不会被感知,因为它是存在与我们的starter中的。所以我们的每个starter都是依赖autoconfigure的:
当然我们也可以把autoconfig的内容直接放在starter包里边。
a. spring-boot-autoconfigure:
注意:这里有个点,就是官网提供的configure大多数在
spring-boot-autoconfigure
包里边,并没有单独创建新包。
b、mybatis-spring-boot-autoconfigure
autoconfigure内容是配置Bean实例到Spring容器的实际代码实现包,然后提供给starter依赖。所以说1.2.3中的b项所说的配置Bean实例到Spring容器中实际是autoconfigure做的,因为是starter依赖它,所以也可以说是starter干的。
所以:
autocinfigure是starter体现出来的能力的代码实现
Spring Boot CLI是一个命令行使用Spring Boot的客户端工具;主要功能如下:
官网2.1
官网2.3
官网2.4
先上个官网文档:Spring Boot CLI 官网文档
因为这个我们用的比较少,所以就不多赘述了。个人感觉比较流脾的功能就是命令行直接执行groovy脚本了。
actuator是Spring Boot的监控插件,本身提供了很多接口可以获取当前项目的各项运行状态指标。
官网文档:Spring Boot actuator 官网文档
名词解释:
Endpoints
: 需要监控的端点。参考官网第二节官网文档
下方的是web工程的端点。
使用方法如下:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
management:
endpoint:
health: ## 开启健康监控端点
enabled: true
beans: ## 开启Bean实例监控端点
enabled: true
浏览器访问(查看监控信息地址
):http://localhost:9500/actuator
查看服务健康状态:
其他API查看官方文档了解或者留言一起研究一下,厚着脸皮我也没怎么用过这个。不过下一章介绍了starter和autoconfigure之后我们就可以去研究actuator的源码了。。。。
本章主要介绍了Spring Boot的四大组件的作用,其中主要是starter和autoconfigure,另外的CLI和actuator用的并不多,所以没有仔细介绍。有想仔细了解的小伙伴可以留言。下一章分析Spring Boot Starter和Autoconfigure的源码实现并实现自己的starter
,敬请期待。。。
上一章:Spring Boot搭建一个WEB应用 & 什么是Spring Boot
下一章:揭开Spring Boot中的Starter神秘面纱并写一个我们自己的Starter