SpringBoot学习(2) - 自定义starter
自己开发一个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的更多相关文章
- SpringBoot系列之自定义starter实践教程
SpringBoot系列之自定义starter实践教程 Springboot是有提供了很多starter的,starter翻译过来可以理解为场景启动器,所谓场景启动器配置了自动配置等等对应业务模块的一 ...
- SpringBoot学习——运行原理学习及自定义Starter pom
例如:pom文件 导入redis jar包 springboot怎么识别和集成? https://blog.csdn.net/flygoa/article/details/68484439 https ...
- SpringBoot起飞系列-自定义starter(十)
一.前言 到现在,我们可以看出来,如果我们想用一些功能,基本上都是通过添加spring-boot-starter的方式来使用的,因为各种各样的功能都被封装成了starter,然后把相关服务注入到容器中 ...
- 尚硅谷springboot学习36-自定义starter
自定义一个starter要引一个依赖,即我们自己写的自动配置,在这个自动配置里写我们的自动配置类,属性类等 自动配置类开始类似这样 @Configuration //指定这个类是一个配置类 @Cond ...
- SpringBoot使用AutoConfiguration自定义Starter
https://segmentfault.com/a/1190000011433487
- SpringBoot自定义Starter实现
自定义Starter: Starter会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦.Starter 提供了一种开箱即用的理念,其中核心就是springboot的自动配置原理相 ...
- SpringBoot Starter机制 - 自定义Starter
目录 前言 1.起源 2.SpringBoot Starter 原理 3.自定义 Starter 3.1 创建 Starter 3.2 测试自定义 Starter 前言 最近在学习Sp ...
- SpringBoot自定义starter开发分布式任务调度实践
概述 需求 在前面的博客<Java定时器演进过程和生产级分布式任务调度ElasticJob代码实战>中,我们已经熟悉ElasticJob分布式任务的应用,其核心实现为elasticjob- ...
- SpringBoot之旅第六篇-启动原理及自定义starter
一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...
随机推荐
- 洛谷$ P$4317 花神的数论题 数位$dp$
正解:数位$dp$ 解题报告: 传送门! 开始看到感觉有些新奇鸭,仔细一想发现还是个板子鸭,,, 考虑设$f_{i}$表示$sum[j]=i$的$j$的个数 日常考虑$dfs$呗,考虑变量要设哪些$Q ...
- 「Luogu P2508」[HAOI2008]圆上的整点 解题报告
题面 给定圆的半径,求圆上整点数 这是一道很Nice的数学题!超爱!好吧,由于这道题,我去Study了一下复数(complex number)复杂的数 真棒!!! 有兴趣的戳这里!!!\(\huge ...
- 在Git的PR(Pull Request)提示冲突无法merge合并的解决方案
问题 假设有一个分支A,向master分支提交PR,然后发生无法自动解决的冲突,PR提示不能执行merge合并. 解决方案1 本地checkout检出并切换到A分支,pull拉取更新到最新代码 在本地 ...
- Linux入门系列1--环境准备及Linux安装
"工欲善其事.必先利其器",本文作为"Linux零基础入门系列"开篇,将完整演示整个开发环境的安装和配置过程,为后续的开发和实验做好基础准备.如果您已安装好环境 ...
- 02_jQuery 验证密码是6位或者8位纯数字
var reg = new RegExp(/^\d{8}$/); //工作密码必须是8位数字 if(!reg.test("12544444").val())) { alert(&q ...
- 2016女生专场 ABCDEF题解 其他待补...
GHIJ待补... A.HUD5702:Solving Order Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...
- Vmware下Ubuntu 14.04静态IP地址的设置方法
一.环境 宿主机 Win 8.1 虚拟机工具 VMware 10.0 虚拟主机系统 Ubuntu 14.04 二.说明 这里需要注意的是:VMware对于VMnet8采用如下规则(192.168. ...
- ArcEngine 创建要素,删除要素,生成网格,渲染图层(VB)
示例代码:https://github.com/yu969890202/ArcEngine/tree/master/WinFrom_ArcEngine_PointDistribution博客后面有两张 ...
- Spring Boot从零入门3_创建Hello World及项目剖析
目录 1 前言 2 名词术语 3 创建Hello World项目 3.1 基于STS4创建项目 3.2 使用Spring Initializr Website创建项目并导入 3.3 基于Spring ...
- python——pickle模块的详解
pickle模块详解 该pickle模块实现了用于序列化和反序列化Python对象结构的二进制协议. “Pickling”是将Python对象层次结构转换为字节流的过程, “unpickling”是反 ...