自己开发一个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. docker容器内存占用过高(例如mysql)

    简介 该文章适用于配置低,特别是内存低的服务器,在用容器部署服务时有可能会因为容器占用内存过高导致服务挂掉时参考解决(不是运行在容器里的话,也是可以修改mysql的配置文件限制内存占用) 最近用doc ...

  2. 微信小程序开发笔记(一)

    一.为什么要学习微信小程序开发 微信小程序是一个可以在微信上打开的轻应用,他是由多个页面组成的程序,跟传统APP比较如下: 优点 1.不需要在应用商店下载,不占用内存空间,即开即用 2.可以在微信内直 ...

  3. surging 社区版本支持.net core 3.1

    简介 surging 经过两年多的研发,微服务引擎已经略有雏形,也承蒙各位的厚爱, GitHub上收获了将近2800星,fork 811,付费用户企业也有十几家,还有咨询培训, 在2020年,我们将依 ...

  4. 2018铁人三项测评题 IOS99

    下面这一部分是我从网上复制过来的, 2.IOS 解题链接:http://ctf4.shiyanbar.com/web/IOS/index.php 这题页面中提示系统升级到了IOS99,我们可以想到修改 ...

  5. spring boot中使用freemarker

    在做Java web开发领域,web模板引擎主要有jsp.freemarker.velocity,其中freemarker是我们常用的一种,在spring boot中使用的freemarker的方法可 ...

  6. python防止字符串转义

    部分转自:https://www.cnblogs.com/hellofengying/p/10183057.html 今天再打开文件名时,出现了错误,如下: In []: path='D:\Code\ ...

  7. 【官方文档翻译】SpringBootActuator生产特性

    翻译自 spring-boot 官方文档(版本:2.2.2.RELEASE) Spring Boot包含了许多附加功能,可以帮助您在将应用程序推送到生产环境时监视和管理它.您可以选择使用HTTP端点或 ...

  8. 【转】Java集合框架面试问题集锦

    Java集合框架(例如基本的数据结构)里包含了最常见的Java常见面试问题.很好地理解集合框架,可以帮助你理解和利用Java的一些高级特性.下面是面试Java核心技术的一些很实用的问题. Q:最常见的 ...

  9. Sql Server学习笔记

    1.指定路径创建数据库 create database student on--创建库的时候必须写 ( name=student, filename='E:\database\student.mdf' ...

  10. DSN

    用户DSN注册信息记录在本机的注册表上 文件DSN保存在本地磁盘上 系统DSN注册在服务器的注册表上,所以客户端连接服务器,只要一台在服务器建立了DSN,其他客户端登录时都会看到该DSN