前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot实战之自定义自动配置

springboot实战之自定义自动配置

作者头像
lyb-geek
发布2019-09-03 11:10:35
1K0
发布2019-09-03 11:10:35
举报
文章被收录于专栏:Linyb极客之路Linyb极客之路

前言

在我们开发的过程中,有时候为了让开发人员更好的进行业务逻辑开发,我们可能会定制开发一个个组件,并起开箱即用的效果。有玩过springboot的朋友可能知道,springboot提供了一系列的starter,这个starter很像就是可插拔的组件,它能够实现自动配置,达到开箱即用,很好的降低了使用框架时的复杂度,让开发人员更容易的使用。今天我们就演示一下如何通过自定义stater来实现一个简单的自动配置例子

自定义starter

1、创建autoconfigure模块项目

这个项目包含需要自动配置的代码逻辑

1.1 pom.xml引入

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

        <dependency>
            <groupId>com.github.lybgeek</groupId>
            <artifactId>springboot-dbtemplate</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <scope>provided</scope>
        </dependency>

其中spring-boot-configuration-processor这个jar,可以让我们在编写application.properties/yml会有智能提示,形如下

springboot-dbtemplate为需要自动配置的具体功能模块,具体实现可以查看

https://github.com/lyb-geek/springboot-learning/tree/master/springboot-dbtemplate


1.2 编写starter自动化配置

@ConfigurationProperties(prefix = "db")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DbProperties {

    /**
     * if you want to use a dbTempate to operate datasouce,dbTemplateEnabled must be true,default false
     */
    private boolean dbTemplateEnabled = false;

    /**
     * jdbc driverClassName must can not be null
     */
    private String driverClassName;

    /**
     * datasource password must can not be null
     */
    private String password;

    /**
     * datasource username must can not be null
     */
    private String username;

    /**
     * datasource url must can not be null
     */
    private String url;
}
@Configuration
@Slf4j
@EnableConfigurationProperties(DbProperties.class)
@ConditionalOnProperty(name="db.db-template-enabled",havingValue = "true")
public class DbTemplateAutoConfiguration {

    @Autowired
    private DbProperties dbProperties;
    @Bean
    @ConditionalOnMissingBean(value= DbTemplate.class)
    public DbTemplate dbTemplate(){
        DbTemplate dbTemplate = new DbTemplate(new QueryRunner(),dataSource());
        return dbTemplate;
    }


    @Bean
    @ConditionalOnMissingBean(value= DataSource.class)
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(dbProperties.getDriverClassName());
        dataSource.setUrl(dbProperties.getUrl());
        dataSource.setUsername(dbProperties.getUsername());
        dataSource.setPassword(dbProperties.getPassword());
        return dataSource;
    }

}

1.3 自定义spring.factories

在src/main/resource目录下创建META-INF目录,并在目录内添加文件spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.github.lybgeek.autoconfigure.dbtemplate.DbTemplateAutoConfiguration
2、另开一个项目,新建一个starter项目

这个项目只用来做依赖导入,可以是一个空jar文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或其它类库,因此可以建一个只含pom.xml文件,不含其它内容的maven项目。其pom.xml内容形如下

<dependencies>
       <dependency>
           <groupId>com.github.lybgeek</groupId>
           <artifactId>springboot-dbtemplate-autoconfigure</artifactId>
           <version>0.0.1-SNAPSHOT</version>
       </dependency>

       <dependency>
           <groupId>com.github.lybgeek</groupId>
           <artifactId>springboot-dbtemplate</artifactId>
           <version>0.0.1-SNAPSHOT</version>
       </dependency>

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-jdbc</artifactId>
       </dependency>
   </dependencies>
3、编写一个引入自定义的starter的项目测试

其测试代码可查看如下链接

https://github.com/lyb-geek/springboot-learning/tree/master/springboot-dbtemplate-test

总结

当你需要实现可插拔、按需集成、开箱即用的组件,可以考虑下模仿一下springboot的starter。另外为了和整个系列的springboot实战项目名称命名统一,所有的项目名称统一都以springboot为前缀打头,因此就没按官方推荐的命名来写starter。官方推荐starter的命名方式是

官方starter都是spring-boot-starter-*,外部的自定义starter推荐使用*-spring-boot-starter

demo链接

https://github.com/lyb-geek/springboot-learning/tree/master/springboot-dbtemplate-autoconfigure https://github.com/lyb-geek/springboot-learning/tree/master/springboot-dbtemplate-starter

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-09-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Linyb极客之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 自定义starter
    • 1、创建autoconfigure模块项目
      • 2、另开一个项目,新建一个starter项目
        • 3、编写一个引入自定义的starter的项目测试
        • 总结
        • demo链接
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档