Spring Boot

Spring Boot Starter的理解和组件化开发

2018-05-31  本文已影响1123人  wyatt_plus

1. 简介

starter是一种服务(或者叫插件)——使得使用某个功能的开发者不需要关注各种依赖库的处理,不需要具体的配置信息,由Spring Boot自动通过classpath路径下的类发现需要的Bean,并织入bean。
简而言之:组件化开发思维,提高代码复用性,避免重复造轮子!!

2.知识点

  • 项目命名方式为[name]-spring-boot-starter (官方命名方式 spring-boot-starter-[name])
  • 在pom.xml中添加starter所需要的依赖
  • 创建starter相关类(至少有一个自动配置类)
  • 在resource文件夹下创建META-INF文件夹 (srping.factories)

3. 实战

网上demo太多,我们基于sharding-jdbc针对springboot的应用分析starter。
即官方sharding-jdbc-spring-boot-starter

3.1 总目录结构

总架构
sharding-jdbc-spring-boot-starter的命名方式符合,也说明此项目不属于springboot官方项目

3.2 配置pom.xml,添加相关依赖

<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>
    <parent>
        <groupId>io.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring</artifactId>
        <version>3.0.0.M1</version>
    </parent>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

核心的autoconfiguration

解释:

auto-configuration,spring-boot有一个@EnableAutoConfiguration注解,他通过读取spring.factories文件里面的EnableAutoConfiguration下面指定的类,来初始化指定类下面的所有加了@bean的方法,并初始化这个bean.

3.3 属性配置类

/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package io.shardingsphere.jdbc.spring.boot.sharding;

import io.shardingsphere.core.yaml.sharding.YamlShardingRuleConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Sharding rule configuration properties.
 *
 * @author caohao
 */
@ConfigurationProperties(prefix = "sharding.jdbc.config.sharding")
public class SpringBootShardingRuleConfigurationProperties extends YamlShardingRuleConfiguration {
}

代码说明:
使用@ConfigurationProperties注解来设置前缀,在application中通过sharding.jdbc.config.sharding.XXX=来设置。

3.4 注册配置

在src/main/resource下新建META-INFO/spring.factories文件。
设置如下即可:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
io.shardingsphere.jdbc.spring.boot.SpringBootConfiguration

如果有多个,逗号分隔即可,如下:

# 例子
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration

3.5 打包发布

mvn clean install

4. 总结

以往单工程结构挺注重功能内部结构的细分以及模块化,但springboot项目后反而很多服务系统内部更为混乱,所以每个公司有必要自己配置自己的starter作为公司的基础架构项目,毕竟重复造轮子很无意义,引用私包也更恐怖!!

上一篇 下一篇

猜你喜欢

热点阅读