Spring Boot starter的典型例子

Spring Boot starter 可以快速引入某个组件(比如redis、pagehelper等),他主要作用是:

  • 在pom文件中导入所要引入组件的依赖信息;
  • 这里边最重要的依赖是这个组件的自动配置类;
  • 其他依赖就比如组件的jar或者组件依赖的jar。

下面以pagehelper组件(一个mybatis的优秀分页插件)为例来介绍starter的结构:

1、pagehelper-spring-boot-starter  jar包的结构:

其中pom文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>
    <parent>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot</artifactId>
        <version>1.2.5</version>
    </parent>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <name>pagehelper-spring-boot-starter</name>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
自动配置类的依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-autoconfigure</artifactId> </dependency>
pagerhelper的依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> </dependencies> </project>

2、pagehelper-spring-boot-autoconfigure  jar的结构如下:

其中比较关键的spring.factories的内容如下(springboot启动时会加载这个文件,进一步加载PageHelperAutoConfiguration类):

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration

PageHelperAutoConfiguration类的内容如下:

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 abel533@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.github.pagehelper.autoconfigure;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Properties;

/**
 * 自定注入分页插件
 *
 * @author liuzh
 */
@Configuration
@ConditionalOnBean(SqlSessionFactory.class)
@EnableConfigurationProperties(PageHelperProperties.class)
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class PageHelperAutoConfiguration {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @Autowired
    private PageHelperProperties properties;

    /**
     * 接受分页插件额外的属性
     *
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = PageHelperProperties.PAGEHELPER_PREFIX)
    public Properties pageHelperProperties() {
        return new Properties();
    }

    @PostConstruct
    public void addPageInterceptor() {
        PageInterceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        //先把一般方式配置的属性放进去
        properties.putAll(pageHelperProperties());
        //在把特殊配置放进去,由于close-conn 利用上面方式时,属性名就是 close-conn 而不是 closeConn,所以需要额外的一步
        properties.putAll(this.properties.getProperties());
        interceptor.setProperties(properties);
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
        }
    }

}

这一步真正将PageInterceptor加入到应用中。

3、总结(比较典型的starter):

  1. starter的作用是将相关的依赖(自动配置类的依赖、组件的依赖)放在pom文件中统一进行引入,这样我们在引入一个组件的时候就只需要引入starter这一个包就可以了。一般starter中是没有java代码的。
  2. 引入的自动配置类,命名一般为***AutoConfiguration(比如PageHelperAutoConfiguration),负责将需要的组件引入到spring中,一般为通过@Bean注入的方式。当然会有很多的默认配置,我们也可以通过配置文件自定义相关的配置。
  3. 经过上述两步就完成了通过starter将组件自动配置进spring容器中。

4、starter的命名规则:

  1. spring 官方开发的组件命名格式:spring-boot-starter-XXX;
  2. 第三方开发的starter命名格式:XXX-spring-boot-starter,其中mybatis-spring-boot-starter也是一个典型的例子。

 

posted @ 2022-02-22 22:12  一首简单的歌  阅读(245)  评论(0编辑  收藏  举报