自定义starter

自定义starter

用途

实际开发中,经常会定义一些公共组件,提供给各个项目团队使用。而在springboot的项目中,一般会将这些公共组件封装为SpringBootstarter

   ****-autoconfigure 提供自动配置功能

   ****-starter 提供依赖管理功能

我们会在 -starter中引入 -autoconfigure功能,这样在引入 -starter就可以了,就相当于把 -autoconfigure也引入进来了

实践:自定义 mybatisstarter

  • 创建 dmybatis-spring-boot-autoconfigure 模块,提供自动配置功能,并自定义配置文件 META-INF/spring/xxx.imports

  • 创建 dmybatis-spring-boot-starter 模块,在 starter 模块中引入自动配置模块

 @AutoConfiguration//表示当前类是一个自动配置类
 public class MybatisAutoConfig {
     //SqlSessionFactoryBean
     @Bean
     public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
         SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
         sqlSessionFactoryBean.setDataSource(dataSource);
         return sqlSessionFactoryBean;
     }
 ​
     //MapperScannerConfigure
     @Bean
     public MapperScannerConfigurer mapperScannerConfigurer(BeanFactory beanFactory){
         MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
         //扫描的包:启动类所在的包及其子包
         List<String> packages = AutoConfigurationPackages.get(beanFactory);
         String p = packages.get(0);
         mapperScannerConfigurer.setBasePackage(p);
 ​
         //扫描的注解:mapper注解
         mapperScannerConfigurer.setAnnotationClass(Mapper.class);
         return mapperScannerConfigurer;
     }
 }
 ​
 ​
 com.htu.config.MybatisAutoConfig
 ​
 ​
 ​

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
 ​
   <groupId>com.htu</groupId>
   <artifactId>DMybatis-Spring-Boot_Starter</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>
 ​
   <name>DMybatis-Spring-Boot_Starter</name>
   <url>http://maven.apache.org</url>
 ​
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
 ​
   <dependencies>
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>3.8.1</version>
       <scope>test</scope>
     </dependency>
 <!--    !!!!!-->
     <dependency>
       <groupId>com.htu</groupId>
       <artifactId>DMybatis-Spring-Boot-AutoConfigre</artifactId>
       <version>1.0-SNAPSHOT</version>
     </dependency>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter</artifactId>
       <version>3.1.2</version>
     </dependency>
 ​
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-jdbc</artifactId>
       <version>3.0.0</version>
     </dependency>
 ​
     <dependency>
       <groupId>org.mybatis</groupId>
       <artifactId>mybatis</artifactId>
       <version>3.5.13</version>
     </dependency>
 ​
     <dependency>
       <groupId>org.mybatis</groupId>
       <artifactId>mybatis-spring</artifactId>
       <version>3.0.0</version>
     </dependency>
 <!--    !!!!!!!!!-->
       <dependency>
           <groupId>org.testng</groupId>
           <artifactId>testng</artifactId>
           <version>RELEASE</version>
           <scope>test</scope>
       </dependency>
   </dependencies>
 </project>

总结

自定义 starter就是自定义启动依赖,方便了项目的开发,自定义starter与引入jar包应该有异曲同工之妙

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ldealing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值