自己开发一个spring boot starter的步骤
1.新建一个项目(全部都基于maven),比如新建一个spring-boot-starter-redis的maven项目

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> <groupId>com.study.spring-boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging> <name>spring-boot-starter-redis</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

2.需要一个配置类,配置类里面需要装配好需要提供出去的类

配置类:

 package com.study.spring_boot_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;
}
}
 package com.study.spring_boot_redis;

 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import redis.clients.jedis.Jedis; @Configuration
@ConditionalOnClass(Jedis.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Jedis Jedis(RedisProperties redisProperties) {
return new Jedis(redisProperties.getHost(),redisProperties.getPort()); }
}

3.
(1)使用@Enable,使用@Import导入需要装配的类

Enable注解:

 package com.study.spring_boot_redis;

 import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import org.springframework.context.annotation.Import; @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(RedisAutoConfiguration.class)
public @interface EnableRedis { }

(2)/META-INF/spring.factories,在org.springframework.boot.autoconfigure.EnableAutoConfiguration配置需要装配的类

/spring-boot-starter-redis/src/main/resources/META-INF/spring.factories:

 org.springframework.boot.autoconfigure.EnableAutoConfiguration = com.study.spring_boot_redis.RedisAutoConfiguration

真正的项目:springbootstarter(maven项目)

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> <groupId>com.study.springboot</groupId>
<artifactId>springboot</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging> <name>springboot</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.3.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>com.study.spring-boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

/springbootstarter/src/main/resources/application.properties:

 redis.host=127.0.0.1
redis.port=6379
 //@EnableRedis
@SpringBootApplication
public class App { public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App.class,args);
Jedis jedis = context.getBean(Jedis.class);
jedis.set("id", "root123");
System.out.println(jedis.get("id"));
context.close();
} }

SpringBoot学习(2) - 自定义starter的更多相关文章

  1. SpringBoot系列之自定义starter实践教程

    SpringBoot系列之自定义starter实践教程 Springboot是有提供了很多starter的,starter翻译过来可以理解为场景启动器,所谓场景启动器配置了自动配置等等对应业务模块的一 ...

  2. SpringBoot学习——运行原理学习及自定义Starter pom

    例如:pom文件 导入redis jar包 springboot怎么识别和集成? https://blog.csdn.net/flygoa/article/details/68484439 https ...

  3. SpringBoot起飞系列-自定义starter(十)

    一.前言 到现在,我们可以看出来,如果我们想用一些功能,基本上都是通过添加spring-boot-starter的方式来使用的,因为各种各样的功能都被封装成了starter,然后把相关服务注入到容器中 ...

  4. 尚硅谷springboot学习36-自定义starter

    自定义一个starter要引一个依赖,即我们自己写的自动配置,在这个自动配置里写我们的自动配置类,属性类等 自动配置类开始类似这样 @Configuration //指定这个类是一个配置类 @Cond ...

  5. SpringBoot使用AutoConfiguration自定义Starter

    https://segmentfault.com/a/1190000011433487

  6. SpringBoot自定义Starter实现

    自定义Starter: Starter会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦.Starter 提供了一种开箱即用的理念,其中核心就是springboot的自动配置原理相 ...

  7. SpringBoot Starter机制 - 自定义Starter

    目录 前言 1.起源 2.SpringBoot Starter 原理 3.自定义 Starter 3.1 创建 Starter 3.2 测试自定义 Starter 前言         最近在学习Sp ...

  8. SpringBoot自定义starter开发分布式任务调度实践

    概述 需求 在前面的博客<Java定时器演进过程和生产级分布式任务调度ElasticJob代码实战>中,我们已经熟悉ElasticJob分布式任务的应用,其核心实现为elasticjob- ...

  9. SpringBoot之旅第六篇-启动原理及自定义starter

    一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...

随机推荐

  1. VBA工程密码破解

    如何破解VBA密码呢? ​ 见过网上很多关于破解VBA工程密码的方式,最常见的如下这种,但其实对于很多版本是不可行的. 基本都会提示“请先对VBA编码设置一个保护密码...” Sub VBAPassw ...

  2. Linux系统下 docker安装命令

    centos sudo: 使用 root 权限登录 Centos.确保 yum 包更新到最新.yum update 卸载旧版本(如果安装过旧版本的话)yum remove docker docker- ...

  3. php strcmp函数漏洞

    strcmp函数漏洞 适用5.3版本以前的php 函数作用:字符串比较 要求传入字符串.如果传入非字符串呢? 结果函数报错!但是函数返回“0”  . 虽然报错了但函数的判断却是“相等” 如何传入非字符 ...

  4. docker练习-群

    了解Swarm集群 群集是一组运行Docker并加入群集的计算机.在此之后,继续运行使用的Docker命令,但现在它们由群集管理器在群集上执行.群中的机器可以是物理的或虚拟的.加入群组后,它们被称为节 ...

  5. C语言联合体(union)的使用方法及其本质-union

    转载自:https://blog.csdn.net/si_zhou_qun_84342712/article/details/53187106 1.联合体union的基本特性——和struct的同与不 ...

  6. spring-boot序章:打造博客系统

    blog 使用spring-boot打造一个博客系统,在项目中学习! 项目功能 文章 游览 创建 编辑 删除 评论 用户 游客 注册用户 关注 被关注 后台统计 注册用户数 在线人数 文章总数 评论总 ...

  7. C#支付宝支付接口H5版(手机网页支付)

    接口官方文档 https://docs.open.alipay.com/203/107090/ 首先在Nuget 安装 Alipay /// <summary>         /// 支 ...

  8. Android学习进度一

    在解决了电脑产生的一系列问题之后成功安装了Android Studio,并在其自带的手机模拟器上成功运行了第一个App(Hello World!),通过这个最简单的App研究了App基本的工程结构,为 ...

  9. PGSQL 字符串作为查询参数的处理

    刚从mysql转到pgsql,不太熟悉用法,今天在查询的时候有一个查询参数是字符串,一直没有这一列的错误 ERROR: column "A 桥梁" does not exist L ...

  10. TensorFlow——Graph的基本操作

    1.创建图 在tensorflow中,一个程序默认是建立一个图的,除了系统自动建立图以外,我们还可以手动建立图,并做一些其他的操作. 下面我们使用tf.Graph函数建立图,使用tf.get_defa ...