springboot之starter

一、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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值