一、mybatis整合starter
1、pom依赖
2、指定自动装配文件位置
3、根据配置文件属性进行装配
4、获取配置文件进行属性绑定
5、springboot启动时加载springFactory
二、自定义starter
1、starter是一个集成接合器,完成两件事:
①引入相关的jar;
②自动配置;
springboot规范:
①starter.jar 完成引入相关的jar
②autoConfigure.jar 完成自动配置
2、starter命名规范
spring提供的starter:
spring-boot-starter-xxx-x.y.z.jar
spring-boot-xxx-autoconfigure-x.y.z.jar
第三方的starter:
xxx-spring-boot-starter-x.y.z.jar
xxx-spring-boot-autoconfigure-x.y.z.jar
3、starter的实现原理
SpringBoot官方的starter,和自定义的starter,基本都是利用java的SPI思想。在SpringBoot的自动装配过程中,最终会加载classpath目录下所有的META-INF/spring.factories文件,查看任一个starter,应该都有该文件。
4、自定义一个第三方lib的starter
(1)准备第三方的jar
(2)制作starter
①创建工程
②pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--这个包已经包括在spring-boot-starter中-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--这个依赖主要可以在代码中轻松的使用@ConfigurationProperties注解注入属性文件配置的属性值-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--引入三方jar-->
③定义一个实体类映射配置信息
//@ConfigurationProperties 不但能映射成String或基本类型的变量。还可以映射为List,Map等数据结构。
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
④定义一个配置类
@Configuration
//该注解是用来开启对@ConfigurationProperties注解配置Bean的支持,也可以在 @ConfigurationProperties 注解的类上添加 @Configuration 或者 @Component 注解
@EnableConfigurationProperties(DemoProperties.class)
//@ConditionalOnProperty 注解控制 @Configuration 是否生效
@ConditionalOnProperty(
prefix = "demo",
name = "isopen",
havingValue = "true"
)
public class DemoConfig {
@Autowired
private DemoProperties demoProperties;
⑤在src\main\resources\META-INF目录下添加spring.factories文件
#-------starter自动装配---------
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.demo.starter.config.DemoConfig