自建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. 时间序列 R 读书笔记 04 Forecasting: principles and practice

    本章開始学习<Forecasting: principles and practice> 1 getting started 1.1 事件的可预言性 一个时间能不能被预言主要取决于以下三点 ...

  2. 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展添加检查订单功能,以便记录并检測输入的參数

    这一章节我们讨论一下扩展添加检查订单功能,以便记录并检測输入的參数. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03 ...

  3. hdu 4059 数论+高次方求和+容斥原理

    http://acm.hdu.edu.cn/showproblem.php? pid=4059 现场赛中通过率挺高的一道题 可是容斥原理不怎么会.. 參考了http://blog.csdn.net/a ...

  4. ThinkPHP使用PHPExcel实现Excel数据导入导出完整实例

    这篇文章主要介绍了ThinkPHP使用PHPExcel实现Excel数据导入导出,非常实用的功能,需要的朋友可以参考下 本文所述实例是使用在Thinkphp的开发框架上,要是使用在其他框架也是同样的方 ...

  5. jquery通过val()取不到textarea中的值

    小编定义了一个textarea控件,却无法根据id取到textarea对象并赋值. 经过实验,得出了原因.代码如下: <!DOCTYPE html> <html> <he ...

  6. hive中的join

    建表 : jdbc:hive2://localhost:10000> create database myjoin; No rows affected (3.78 seconds) : jdbc ...

  7. Log4E插件使用记录

    在Java编程中,为了调试使用一大堆的System.out.println()或者是System.err.println查看程序的执行,最后由于懒得注释导致发布正式版时需要遍历并注释.而大量的Syst ...

  8. phpcms v9 配置sphinx全文索引教程

    英文介绍:http://www.sphinxsearch.com/docs/manual-0.9.9.html 一.首先需要在服务器上安装sphinx 在Windows上安装sphinx     1. ...

  9. hdu 2460(tarjan求边双连通分量+LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2460 思路:题目的意思是要求在原图中加边后桥的数量,首先我们可以通过Tarjan求边双连通分量,对于边 ...

  10. mybatis if test 相等的情况怎样动态拼接sql

    今天程序须要依据前台的传过来的状态推断在数据库里是取 where a>b 还是 a<b 还是 a=0 的情况  搞了一下午最后试了下 在if 里面拼接  #{status}=#{statu ...