SpringBoot学习(2) - 自定义starter

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

pom.xml:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4
 5     <groupId>com.study.spring-boot</groupId>
 6     <artifactId>spring-boot-starter-redis</artifactId>
 7     <version>1.0.0</version>
 8     <packaging>jar</packaging>
 9
10     <name>spring-boot-starter-redis</name>
11     <url>http://maven.apache.org</url>
12
13     <properties>
14         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15     </properties>
16
17     <dependencies>
18         <dependency>
19             <groupId>org.springframework.boot</groupId>
20             <artifactId>spring-boot-starter</artifactId>
21             <version>1.5.3.RELEASE</version>
22         </dependency>
23         <dependency>
24             <groupId>redis.clients</groupId>
25             <artifactId>jedis</artifactId>
26             <version>2.9.0</version>
27         </dependency>
28         <dependency>
29             <groupId>junit</groupId>
30             <artifactId>junit</artifactId>
31             <version>3.8.1</version>
32             <scope>test</scope>
33         </dependency>
34     </dependencies>
35 </project>

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

配置类:

 1 package com.study.spring_boot_redis;
 2
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4
 5 @ConfigurationProperties(prefix="redis")
 6 public class RedisProperties {
 7     private String host;
 8     private Integer port;
 9     public String getHost() {
10         return host;
11     }
12     public void setHost(String host) {
13         this.host = host;
14     }
15     public Integer getPort() {
16         return port;
17     }
18     public void setPort(Integer port) {
19         this.port = port;
20     }
21 }
 1 package com.study.spring_boot_redis;
 2
 3
 4 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 5 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 6 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.context.annotation.Configuration;
 9
10 import redis.clients.jedis.Jedis;
11
12 @Configuration
13 @ConditionalOnClass(Jedis.class)
14 @EnableConfigurationProperties(RedisProperties.class)
15 public class RedisAutoConfiguration {
16     @Bean
17     @ConditionalOnMissingBean
18     public Jedis Jedis(RedisProperties redisProperties) {
19         return new Jedis(redisProperties.getHost(),redisProperties.getPort());
20
21     }
22 }

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

Enable注解:

 1 package com.study.spring_boot_redis;
 2
 3 import java.lang.annotation.Documented;
 4 import java.lang.annotation.ElementType;
 5 import java.lang.annotation.Retention;
 6 import java.lang.annotation.RetentionPolicy;
 7 import java.lang.annotation.Target;
 8
 9 import org.springframework.context.annotation.Import;
10
11 @Target(ElementType.TYPE)
12 @Retention(RetentionPolicy.RUNTIME)
13 @Documented
14 @Import(RedisAutoConfiguration.class)
15 public @interface EnableRedis {
16
17 }

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

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

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

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

pom.xml:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4
 5     <groupId>com.study.springboot</groupId>
 6     <artifactId>springboot</artifactId>
 7     <version>1.0.0</version>
 8     <packaging>jar</packaging>
 9
10     <name>springboot</name>
11     <url>http://maven.apache.org</url>
12
13     <properties>
14         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15     </properties>
16
17     <dependencyManagement>
18         <dependencies>
19             <dependency>
20                 <groupId>org.springframework.boot</groupId>
21                 <artifactId>spring-boot-dependencies</artifactId>
22                 <version>1.5.3.RELEASE</version>
23                 <scope>import</scope>
24                 <type>pom</type>
25             </dependency>
26         </dependencies>
27     </dependencyManagement>
28
29     <dependencies>
30         <dependency>
31             <groupId>com.study.spring-boot</groupId>
32             <artifactId>spring-boot-starter-redis</artifactId>
33             <version>1.0.0</version>
34         </dependency>
35         <dependency>
36             <groupId>junit</groupId>
37             <artifactId>junit</artifactId>
38             <scope>test</scope>
39         </dependency>
40     </dependencies>
41 </project>

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

1 redis.host=127.0.0.1
2 redis.port=6379
 1 //@EnableRedis
 2 @SpringBootApplication
 3 public class App {
 4
 5     public static void main(String[] args) {
 6         ConfigurableApplicationContext context = SpringApplication.run(App.class,args);
 7         Jedis jedis = context.getBean(Jedis.class);
 8         jedis.set("id", "root123");
 9         System.out.println(jedis.get("id"));
10         context.close();
11     }
12
13 }
时间: 2024-08-06 20:08:37

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

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

Springboot是有提供了很多starter的,starter翻译过来可以理解为场景启动器,所谓场景启动器配置了自动配置等等对应业务模块的一个工程,有需要时候直接引入项目就可以,比如需要使用rabbitMQ,直接引入spring-boot-starter-activemq既可,详细介绍可以参考Springboot官方文档关于starters的介绍 查看官方文档,可以找到如下的命名规范: 其意思是SpringBoot官方的starter命名要定义为spring-boot-starter-*,自

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

自定义一个starter要引一个依赖,即我们自己写的自动配置,在这个自动配置里写我们的自动配置类,属性类等 自动配置类开始类似这样 @Configuration //指定这个类是一个配置类 @ConditionalOnXXX //在指定条件成立的情况下自动配置类生效 @AutoConfigureAfter //指定自动配置类的顺序 @Bean //给容器中添加组件 @ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置 @EnableConfigura

springboot核心技术(四)-----Docker、数据访问、自定义starter

Docker 1.简介 Docker是一个开源的应用容器引擎:是一个轻量级容器技术: Docker支持将软件编译成一个镜像:然后在镜像中各种软件做好配置,将镜像发布出去,其他使用者可以直接使 用这个镜像: 运行中的这个镜像称为容器,容器启动是非常快速的. 2.核心概念 docker主机(Host):安装了Docker程序的机器(Docker直接安装在操作系统之上): docker客户端(Client):连接docker主机进行操作: docker仓库(Registry):用来保存各种打包好的软件

Springboot学习记录1--概念介绍以及环境搭建

摘要:springboot学习记录,环境搭建: 官方文档地址:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/ 本机为Ubuntu 概念:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,Spring Boot致力于在蓬勃发展的快速

SpringBoot学习-SpringMVC自动配置

SpringBoot学习-SpringMVC自动配置 前言 在SpringBoot官网对于SpringMVCde 自动配置介绍 1-原文介绍如下: Spring MVC Auto-configuration Spring Boot provides auto-configuration for Spring MVC that works well with most applications. The auto-configuration adds the following features

Springboot学习05-自定义错误页面完整分析

Springboot学习06-自定义错误页面完整分析 前言 接着上一篇博客,继续分析Springboot错误页面问题 正文 1-自定义浏览器错误页面(只要将自己的错误页面放在指定的路径下即可) 1-1-Springboot错误页面匹配机制(以404错误为例): 1-在模板引擎下:找templates/error/404.html;如果没有,则继续匹配 2-在模板引擎下:找templates/error/4XX.html;如果没有,则继续匹配 3-在静态资源下:找static/error/404.

小代学Spring Boot之自定义Starter

想要获取更多文章可以访问我的博客?-?代码无止境. 使用Spring Boot框架一段时间之后的小代同学,发现在Spring Boot项目中经常会引入各种各样的Starter,例如Web项目的spring-boot-starter-web以及集成MyBatis时的mybatis-spring-boot-starter.那么这个Starter到底是些什么呢? 什么是Starter 经过一番研究,小代同学了解到Starter主要是Spring Boot用来简化项目依赖的一种形式,比如spring-b

SpringBoot中Thymeleaf自定义配置项

SpringBoot中Thymeleaf自定义配置项 SpringBoot在模板引擎中支持Thymeleaf自定义可配置项,其配置文件名必须是messages.properties 学习视频: http://www.itlaoqi.com/chapter/1688.html 源码地址: QQ群 814077650 , 群共享中自助下载 老齐的官网: itlaoqi.com (更多干货就在其中) messages.properties app.report.name=SAMDI上海分公司员工信息表

spring boot自定义starter

使用spring boot开发微服务后,工程的数量大大增加(一定要按照领域来切,不要一个中间件客户端包一个),让各个jar从开发和运行时自包含成了一个重要的内容之一.spring boot starter就可以用来解决该问题(没事启动时别依赖于applicationContext.getBean获取bean进行处理,依赖关系太折腾,有时候在复杂系统中解决此事比较麻烦,需要修改开源框架代码才能实现,反过来修改开源源码后,维护也是个麻烦事).言归正传,说说自定义starter.首先请熟悉spring