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依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...
随机推荐
- 3.24 7.13 Python基础汇总
对象类型 类型名称 示例 简要说明 备注 数字 int,float,complex 1234,3.14,1.3e5,3+4j 数字大小没有限制 十六进制用0x前缀和0-9,a-f表示 字符串 str ...
- 洛谷$P2057\ [SHOI2007]$ 善意的投票 网络流
正解:网络流 解题报告: 传送门! $umm$看到每个人要么0要么1就考虑最小割呗,,,? 然后贡献有两种?一种是违背自己的意愿,一种是和朋友的意愿违背了 所以考虑开一排点分别表示每个人,然后$S$表 ...
- 还看不懂同事代码?快来补一波 Java 7 语法特性
前言 Java 平台自出现到目前为止,已经 20 多个年头了,这 20 多年间 Java 也一直作为最流行的程序设计语言之一,不断面临着其他新兴编程语言的挑战与冲击.Java 语言是一种静态强类型语言 ...
- Java线程池学习总结
一 使用线程池的好处 池化技术相比大家已经屡见不鲜了,线程池.数据库连接池.Http 连接池等等都是对这个思想的应用.池化技术的思想主要是为了减少每次获取资源的消耗,提高对资源的利用率. 线程池提供了 ...
- windows生成github密钥并推送文件踩坑
强调官方文档最可靠,百度踩坑很浪费时间,建议去寻找一手数据源头 github官方文档提供了帮助 第一步 查看密钥 如果您还没有 SSH 密钥,则必须生成新 SSH 密钥. 如果您不确定是否已有 SSH ...
- AcWing 251. 小Z的袜子| 分块+莫队
传送门 题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿. 终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命. 具体来说,小Z把这N只袜子从 ...
- 【转】最简单的安装pip的方法
网上有各种方法安装pip,针对不同的系统方法还不一样,最后发现还是下面这种方法最简单,直接了当干脆方便,适用于Windows和Linux. (1)下载pip 进入https://pypi.python ...
- dp-(LCS 基因匹配)
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19885 Accepted: ...
- 使用ABAP Data Validator验证数据有效性
在日常的开发过程中,我们常常要处理不同来源的数据.数据可能来自不可靠的外部系统.不可靠的用户输入和甚至设计有误的数据库表,因此,对数据有效性进行验证是必要的工作. 开源工具ABAP Data Vali ...
- flask模板 flask-bootstrap
1.模板 a.block块中继承前面block块的内容,需要添加{{super()}} b.macro 宏: 作用:在模板中定义函数(定义函数->注意添加()->可以使用from 模板 ...