自建spring-boot-starter

  artifactId命名  

    Spring 官方 Starter通常命名为spring-boot-starter-{name}如 spring-boot-starter-web,

    Spring官方建议非官方Starter命名应遵循{name}-spring-boot-starter的格式。

示例以redis为例

1》新建一个maven项目redis-spring-boot-starter,添加pom

        <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>

2》添加配置属性

package com.lhx.spring.redis;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "redis")
public class RedisProperties {
private String host;
private Integer port; public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public Integer getPort() {
return port;
} public void setPort(Integer port) {
this.port = port;
} }

3》添加AutoConfiguration类

@Configuration
@ConditionalOnClass(Jedis.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(Jedis.class)
public Jedis jedis(RedisProperties redisProperties) {
return new Jedis(redisProperties.getHost(), redisProperties.getPort().intValue());
}
}

此时程序使用。

4》程序使用

  添加依赖包

        <dependency>
<groupId>com.lhx.spring</groupId>
<artifactId>redis-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>

具体分为两种:

方式一、在redis-spring-boot-starter添加注解方式,Import导入配置类

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(RedisAutoConfiguration.class)
public @interface EnableRedis {
}

程序使用

@EnableRedis
@SpringBootApplication
public class App {
public static void main(String[] args) throws SQLException {
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
Jedis jedis = context.getBean(Jedis.class);
jedis.set("id","3333");
System.out.println(jedis.get("id"));
context.close();
}
}

方式二、spring.factories,配置配置类

  查看在spring-boot-autoconfigure-1.5.9.RELEASE.jar包中META-INF下的spring.factories文件

  一般都是在starter项目的resources/META-INF文件夹下的spring.factories文件中加入需要自动化配置类的全限定名称。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.lhx.spring.redis.RedisAutoConfiguration

  spring boot项目中的EnableAutoConfigurationImportSelector会自动去每个jar的相应文件下查看spring.factories文件内容,并将其中的类加载出来在auto-configuration过程中进行配置。而EnableAutoConfigurationImportSelector在@EnableAutoConfiguration注解中被import。

程序使用

@SpringBootApplication
public class App {
public static void main(String[] args) throws SQLException {
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
Jedis jedis = context.getBean(Jedis.class);
jedis.set("id","4444");
System.out.println(jedis.get("id"));
context.close();
}
}

018-Spring Boot Starter开发的更多相关文章

  1. spring boot starter开发

    作为公司的技术保障部,一直承担着技术方向的把控,最近公司准备全面转入spring boot的开发.所以我们部门也一直在调研相关的技术知识点: 使用springboot开发应用已经有一段时间了,我们都沉 ...

  2. 最详细的自定义Spring Boot Starter开发教程

    1. 前言 随着Spring的日渐臃肿,为了简化配置.开箱即用.快速集成,Spring Boot 横空出世. 目前已经成为 Java 目前最火热的框架了.平常我们用Spring Boot开发web应用 ...

  3. Spring Boot Starter 开发指南

    Spring Boot Starter是什么? 依赖管理是任何复杂项目的关键部分.以手动的方式来实现依赖管理不太现实,你得花更多时间,同时你在项目的其他重要方面能付出的时间就会变得越少. Spring ...

  4. 从零开始开发一个Spring Boot Starter

    一.Spring Boot Starter简介 Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件 ...

  5. Spring Boot Starter 介绍

    http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...

  6. spring -boot s-tarter 详解

    Starter POMs是可以包含到应用中的一个方便的依赖关系描述符集合.你可以获取所有Spring及相关技术的一站式服务,而不需要翻阅示例代码,拷贝粘贴大量的依赖描述符.例如,如果你想使用Sprin ...

  7. spring boot + vue + element-ui全栈开发入门——spring boot后端开发

    前言 本文讲解作为后端的spring boot项目开发流程,如果您还不会配置spring boot环境,就请点击<玩转spring boot——快速开始>,如果您对spring boot还 ...

  8. SpringBoot 之Spring Boot Starter依赖包及作用

    Spring Boot 之Spring Boot Starter依赖包及作用 spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. spri ...

  9. Spring Boot Starter列表

    转自:http://blog.sina.com.cn/s/blog_798f713f0102wiy5.html Spring Boot Starter 基本的一共有43种,具体如下: 1)spring ...

  10. 手把手教你定制标准Spring Boot starter,真的很清晰

    写在前面 我们每次构建一个 Spring 应用程序时,我们都不希望从头开始实现具有「横切关注点」的内容:相反,我们希望一次性实现这些功能,并根据需要将它们包含到任何我们要构建的应用程序中 横切关注点 ...

随机推荐

  1. PHP 命名空间namespace 和 use

    慕课网教程: http://www.imooc.com/video/7834 PHP 中命名空间的概念和高级语言(如C#.JAVA)有很大的差异,一度让我混淆甚至怀疑它存在的意义和目的. 今天找时间学 ...

  2. linux使用fdisk命令操作硬盘

    知识点: MBR:Master Boot Record 主引导记录 在硬盘0柱面 0磁头的第一个扇区,占512字节(3部分 主引导程序 446字节,硬盘分区表DPT[disk partion tabl ...

  3. Backup and Recovery Basics1

    一.Backup and Recovery Overview 1.Backup and Recovery Overview 1.1 What is Backup and Recovery? 一般,备份 ...

  4. form表单右边弹窗提示不能为空

    if (key == null || key == "") { layer.tips('流程标识key不能为空', $('#search_input'), { tips: [3, ...

  5. Unity3d中使用自带动画系统制作下雨效果(一)

    之前看了以前版本的unity3d demo AngryBots ,觉得里面的下雨效果不错,刚好前段时间学习了,写出来跟大家分享下,直接开始. 使用自带动画系统制作下雨效果. 先制作下雨的雨滴涟漪. 步 ...

  6. 小米Note全网通支持7模19频:先发标准版

    2015-06-26 16:42:53 17749 次阅读 9 次推荐 稿源:安卓中国 43 条评论 感谢安卓中国的投递 自古一入电信深似海,从此手机没法买.现在首台全网通小米手机即将诞生.6 月 2 ...

  7. Linux 比较重要且难掌握命令 集合

    1. find find path –option [-print] [-exec command] {} \; find . -maxdepth 1 -name aa find . -maxdept ...

  8. nc 查看端口是否 联通

    nc 47.9.16.1 3306 如果卡住,说明 该IP的这个端口 访问不通, 防火墙拦截了

  9. 使用transform实现手风琴布局

  10. Youth Is Not a Time of Life

    Youth is not a time of life; it is a state of mind.青春不是年华,而是心境: It is not a matter of rosy cheeks, r ...