个人博客网:https://wushaopei.github.io/    (你想要这里多有)

在实际项目开发中,我们常常会用到各种各样的 starter,这些starter 有的是有 springboot官方提供并已经整合一些基本功能的,如:spring-boot-starter,也有一些是由 第三方将框架与springboot进行定制整合后提供给我们进行快捷高效开发的,如:MyBatis-Spring-Boot-Starter

在日常开发中,由于项目的要求以及高效开发、部署等,我们可以对springboot进行私有定制,生成自己需要的启动器,再引入到实际项目中,提高开发的效率。

举例: 我们在springboot 中使用 redis 的 java控件jedis 时,需要 进行创建  Jedis 的实例并传入相应参数如  ip 、port 等,在使用springboot后,可以进行定制化starter,将Jedis 用ip、port 进行实例化,并将 Bean 注入容器由 SpringBoot 进行管理,制成启动器。

在需要使用到 redis 功能的工程项目中引入该启动器,这样就能够提高开发的效率。

正规的starter是一个独立的工程,然后在maven中新仓库注册发布,其他开发人员就可以使用你的starter了。

以下实现一个针对 redis 的 定制器 starter。

一、新建一个maven项目spring-boot-starter-redis (启动器)

引入如下依赖:

                <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!--自动配置依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

二、在此项目中编写 RedisProperties.class ,用以从 application.properties 中读取 redis 的配置信息

package com.example.demo.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; /**
* @ClassName JedisProperties
* @Description TODO
* @Author wushaopei
* @Date 2020/2/2 18:30
* @Version 1.0
*/
@Component
@ConfigurationProperties(prefix="jedis")
public class RedisProperties {
private String host;
private int port; public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public int getPort() {
return port;
} public void setPort(int port) {
this.port = port;
} }

三、在此项目中编写RedisAutoConfiguration.class,用以将 Jedis 的bean 装载进 spring 容器中

package com.example.demo.autoConfigure;

import com.example.demo.properties.RedisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import javax.annotation.PostConstruct; /**
* @ClassName JedisAutoConfigure
* @Description TODO
* @Author wushaopei
* @Date 2020/2/2 18:32
* @Version 1.0
*/
@Configuration
@ConditionalOnClass({ Jedis.class})
@EnableConfigurationProperties({RedisProperties.class})
@AutoConfigureAfter({RedisProperties.class})
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Jedis Jedis(RedisProperties redisProperties) {
//spring会自动将RedisProperties这个bean注入进来,
return new Jedis(redisProperties.getHost(),redisProperties.getPort(),10000);
}
}

为大家解释一下这些注解的含义

四、在此项目中resource目录下新建 application.properties 文件

redis.host=127.0.0.1
redis.port=6379

五、Bean 注入方式:

第一种:自动注入,创建 依赖配置 spring.factories

在resource 目录下新建 META-INF 目录,并在该目录下创建 spring.factories 文件,在文件添加以下数据:

org.springframework.boot.autoconfigure.EnableAutoConfiguration =com.example.demo.autoConfigure.RedisAutoConfiguration

当前数据用于将加载生成的Bean 交给当前启动器所在的容器进行管理,并在被引入到具体的工程项目时被项目中的容器所扫描到而调用。

第二种:手动注入,创建EnableRedis

根据@Import注解 的作用进行切面配置,手动添加到 工程项目启动类

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

使用方式:

@SpringBootApplication
@EnableRedis//关键的一步
public class BlogApplication {
......
}

六、新建Blog 工程项目,引入 spring-boot-starter-redis 依赖

        <dependency>
<groupId>com.example</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

七、在Blog项目中,编写启动类BlogApplication

package com.imooc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext;
import redis.clients.jedis.Jedis; /**
* @ClassName BlogApplication
* @Description TODO
* @Author wushaopei
* @Date 2020/2/2 23:15
* @Version 1.0
*/
@SpringBootApplication
public class BlogApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(BlogApplication.class, args);
Jedis jedis = context.getBean(Jedis.class);
System.out.println(jedis);
//如果成功连接上了redis,jedis.ping()会返回一个pong
System.out.println(jedis.ping());
}
}

八、启动redis

启动虚拟机或云服务器的redis ,

[root@wsp /]# redis-server /etc/redis.conf
[root@wsp /]# redis-cli

先启动 Spring-boot-starter-redis 启动器 ,再启动具体的项目工程 spring-boot-starter-apps

SpringBoot 定制 starter 启动器的更多相关文章

  1. springboot 自定义starter之AutoConfiguration【原】

    八.自定义starter AutoConfiguration: 1.这个场景需要使用到的依赖是什么? 没有特别依赖的配置 2.如何编写自动配置 @Configuration //指定这个类是一个配置类 ...

  2. SpringBoot --- 自定义 Starter

    SpringBoot --- 自定义 Starter 创建 1.需要创建一个新的空工程 2.新的工程需要引入两个模块 一个Maven 模块 作为启动器 一个SpringBoot 模块 作为自动配置模块 ...

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

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

  4. SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf 4节课

    1.SpringBoot Starter讲解 简介:介绍什么是SpringBoot Starter和主要作用 1.官网地址:https://docs.spring.io/spring-boot/doc ...

  5. 如何封装springboot的starter

    --为啥要封装starter --如何封装 --测试 为啥要封装starter springboot的starter开箱即用,只需要引入依赖,就可以帮你自动装配bean,这样可以让开发者不需要过多的关 ...

  6. 手撸一个SpringBoot的Starter,简单易上手

    前言:今天介绍一SpringBoot的Starter,并手写一个自己的Starter,在SpringBoot项目中,有各种的Starter提供给开发者使用,Starter则提供各种API,这样使开发S ...

  7. 【SpringBoot】01.创建Springboot项目及启动器

    创建Springboot项目及启动器 1.创建一个简单maven项目 SpringBoot2.0以下需要使用JDK1.7 ,2.0以上使用JDK1.8 如果需要修改JDK的版本需要打开pom文件: & ...

  8. SpringBoot的starter到底是什么?

    前言 我们都知道,Spring的功能非常强大,但也有些弊端.比如:我们需要手动去配置大量的参数,没有默认值,需要我们管理大量的jar包和它们的依赖. 为了提升Spring项目的开发效率,简化一些配置, ...

  9. 170710、springboot编程之启动器Starter详解

    此文系参考网络大牛的,如有侵权,请见谅! Spring Boot应用启动器基本的一共有N(现知道的是44)种:具体如下: 1)spring-boot-starter 这是Spring Boot的核心启 ...

随机推荐

  1. Synchronized加锁、锁升级和java对象内存结构

    首先了解一下JMM中定义的内存操作: 一个线程操作数据时候都是从主内存(堆内存)读取到自己工作内存(线程私有的数据区域)中再进行操作.对于硬件内存来说,并没有工作内存和主内存的区分,这都是java内存 ...

  2. [hdu5203]计数水题

    思路:把一个木棍分成3段,使之能够构成三角形的方案总数可以这样计算,枚举一条边,然后可以推公式算出当前方案数.对于已知一条边的情况,也用公式推出.用max和min并维护下,以减少情况数目. #prag ...

  3. android 压缩图片大小,防止OOM

    android开发中,图片的处理是非常普遍的,经常是需要将用户选择的图片上传到服务器,但是现在手机的分辨率越来越好了,随便一张照片都是2M或以上,如果直接显示到ImageView中,是会出现OOM的, ...

  4. Python基础语法day_03——列表

    day_03 列表是什么 在Python中,用[]来表示列表,并用逗号来分隔其中的元素.下面是一个简单的列表示例: >>> bicycles = ['treak','cannonda ...

  5. git命令之切换分支

    Git一般有很多分支,我们clone到本地的一般都是master分支,如何进行分支的切换呢?那么下面带大家简单的看看如何通过命令来切换: 1.查看远程仓库及本地的所有分支 命令:git branch ...

  6. vue中mixins的使用方法和注意点(详2)(异步请求的情况)

    当混合里面包含异步请求函数,而我们又需要在组件中使用异步请求函数的返回值时,我们会取不到此返回值,如下: mixin中 组件中 控制台 解决方案:不要返回结果而是直接返回异步函数 mixin中 组件中 ...

  7. 腾讯几款QQ软件

    1.QQ(普通版QQ) https://im.qq.com/ 2.Tim(QQ办公简洁版) https://tim.qq.com/ https://baike.baidu.com/item/Tim/2 ...

  8. 【Python代码】TSNE高维数据降维可视化工具 + python实现

    目录 1.概述 1.1 什么是TSNE 1.2 TSNE原理 1.2.1入门的原理介绍 1.2.2进阶的原理介绍 1.2.2.1 高维距离表示 1.2.2.2 低维相似度表示 1.2.2.3 惩罚函数 ...

  9. zz MySQL redo log及recover过程浅析

    原作地址:http://www.cnblogs.com/liuhao/p/3714012.html 写在前面:作者水平有限,欢迎不吝赐教,一切以最新源码为准. InnoDB redo log 首先介绍 ...

  10. 基于SpringCloud分布式架构

    基于SpringCloud分布式架构 为什么要使用分布式架构 Spring Cloud 专注于提供良好的开箱即用经验的典型用例和可扩展性机制覆盖 分布式/版本化配置 服务注册和发现 路由 Servic ...