Spring Boot起步依赖:定制starter

在定制我们自己的起步依赖——xxx.spring.boot.starter之前,我们先了解下什么是Spring Boot起步依赖。

起步依赖,是Spring Boot带给我们的一项重要的便利。要理解Spring Boot起步依赖带来的好处,先让我们假设它不存在。如果没用Spring Boot的话,你会向项目里添加哪些依赖呢?要用Spring MVC的话,你需要哪个Spring依赖?你还记得Thymeleaf的Group和Artifact ID吗?你应该用哪个版本的Spring Data JPA呢?它们放在一起兼容吗?

总之,如果没有Spring Boot起步依赖的话,你就需要自己管理项目依赖关系。Spring Boot通过提供众多起步依赖降低项目依赖的复杂度。起步依赖本质上是一个Maven项目对象模型(Project Object Model,POM),定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。

创建自己的起步依赖Starter

一个完整的Spring Boot 起步依赖一般包括两部分:

  • autoconfigure 模块,包含着自动配置的代码
  • starter 模块,包含 autoconfigure 模块的依赖以及其它有用的依赖。

命名规则

为起步依赖提供一个恰当的命名空间是一个好的习惯。通常我们的习惯是xxx-spring-boot-autoconfigure,假设要为了jedis创建一个起步依赖,那么一个比较好的命名方式是jedis-spring-boot-autoconfigurejedis-spring-boot-starter

autoconfigure模块

起步依赖的大部分内容都在autoconfigure模块,通常包括:

一个被 @ConfigurationProperties 注解的属性定义类。如:

@Component
@ConfigurationProperties(prefix="spring.jedis")
public class JedisProperties {
    private String host;
    private int port;
    private int maxTotal=10;
    private int maxIdle=5;
    private int minIdle=5;
    private boolean testOnBorrow=false;
    private boolean testOnReturn=false;
    private int timeout;
    
    // getter and setter
    ...
}

一个用于实现自动配置的AutoConfigure类。例如:

@Configuration
@ConditionalOnClass({ JedisPool.class, Jedis.class, JedisPoolConfig.class})
@EnableConfigurationProperties({JedisProperties.class})
@AutoConfigureAfter({JedisProperties.class})
public class JedisAutoConfigure {
    @Autowired
    private JedisProperties jedisProperties;
    @PostConstruct
    public void checkConfigFileExists()
    {
    }
    @Bean
    @ConfigurationProperties(prefix = "spring.jedis")
    public JedisPoolConfig jedisPoolConfig(){
        return new JedisPoolConfig();
    }
    @Bean
    @ConditionalOnMissingBean
    public JedisPool jedisPool(JedisPoolConfig jedisPoolConfig){
        return new JedisPool(jedisPoolConfig,
                jedisProperties.getHost(),
                jedisProperties.getPort(),
                jedisProperties.getTimeout());
    }
}

一个指定AutoConfigure类路径的文件 META-INF/spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
fun.example.spring.boot.autoconfigure.JedisAutoConfigure

这个三个文件差不多就是autoconfigure模块的全部了。

除此之外,还可以配置META-INF/spring-autoconfigure-metadata.properties元数据文件,这个文件可以提高应用的启动速度。官方原文如下:

Spring Boot uses an annotation processor to collect the conditions on auto-configurations in a metadata file (META-INF/spring-autoconfigure-metadata.properties). If that file is present, it is used to eagerly filter auto-configurations that do not match, which will improve startup time. It is recommended to add the following dependency in a module that contains auto-configurations:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

starter 模块

starter 模块完完全全是空的,唯一的目的就是提供必需的依赖。它也许长成这样:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>jedis-spring-boot</artifactId>
        <groupId>fun.example.spring.boot</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>jedis-spring-boot-autoconfigure</artifactId>
    <name>jedis-spring-boot-autoconfigure</name>
    <properties>
        <jedis.version>2.8.2</jedis.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>${jedis.version}</version>
        </dependency>
    </dependencies>
</project>

整个项目的配置模板可以看这里:spring-boot-demo

猜你喜欢

转载自www.cnblogs.com/bluemilk/p/10577578.html