SpringBoot(二十三)自定义starter

SpringBoot版本:2.1.1

目录

自定义Starter

使用

测试代码


自从使用SpringBoot以来,我就爱了,相对于Spring,繁琐的xml配置,SpringBoot是开箱即用,当然主要是因为它的自动配置,也就是@EnableAutoConfiguration,注解原理前面已经解析过了(传送门),并在此基础上自定义了一个注解实现bean装配(传送门)。

自定义Starter

今天再自定义实现一个Starter,简单实现jedis的自动配置,项目命名就跟随官方命名方式,第三方Starter,所以redis写前面,跟mybatis-spring-boot-starter是一个理。项目目录如下:

创建一个Maven项目,是jar,不是war,导入依赖:

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>  

<dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
		<version> 2.1.1.RELEASE </version> 
  	</dependency>

        <!-- 想要让自定义配置项有提示,导入这个包,在项目编译后会生成元数据,同样是在META-INF目录下-->        
        <!-- 会自动生成spring-configuration-metadata.json文件。--> 
	<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <version> 2.1.1.RELEASE </version> 
        </dependency>
  	<dependency>
  		<groupId>redis.clients</groupId>
  		<artifactId>jedis</artifactId>
  		<version>2.9.0</version>
  	</dependency>
  </dependencies>

首先创建一个JedisProperties配置类,里面就是redis的配置项,host跟port。

package com.springboot.redis;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix="redis")
public class JedisProperties {
	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;
	}
}

 在application.properties文件中使用的时候,如下配置:

redis.host=192.168.52.145
redis.port=6379

然后创建一个自动配置类,自动配置Jedis。

package com.springboot.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(JedisProperties.class)
public class JedisAutoConfiguration {
	
	@Bean
	@ConditionalOnMissingBean
	public Jedis jedis(JedisProperties redisProperties) {
		return new Jedis(redisProperties.getHost(),redisProperties.getPort());
	}
}

类注释:

@Configuration:标识是一个配置类

@ConditionalOnClass(Jedis.class):存在Jedis时装配,也就是说类路径中有这个类就装配

@EnableConfigurationProperties(JedisProperties.class):启用对@ConfigurationProperties注释bean的支持,即注入     bean JedisProperties,使配置生效

方法注释:

@Bean:标识该方法生成一个由Spring容器管理的bean

@ConditionalOnMissingBean:在beanFactory中没有满足指定要求的bean时才匹配,即不存在Jedis时匹配

 

还有最后一步,也是最重要一步,在META-INF / spring.factories文件中注册JedisAutoConfiguration,如下:


org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.springboot.redis.JedisAutoConfiguration

到这自定义Starter就完成了,项目上右键,Run as--> maven install 一下,就ok了。

使用

新建一个maven项目,只要在pom文件中添加依赖就好了。

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

<!-- 测试用 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

添加redis配置

redis.host=192.168.52.145
redis.port=6379

 可以看到我们自定义的配置项是有提示的。

生成的元数据文件 spring-configuration-metadata.json,对文件内容有兴趣的可以自行去查看哦。

测试代码

package com.eastcom;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import redis.clients.jedis.Jedis;

@RunWith(SpringRunner.class)
@SpringBootTest
public class JedisTest {
	
	@Autowired
	private Jedis jedis;
	
	@Test
	public void show() {
		System.out.println(jedis.ping());
		jedis.set("starter", "myJedis");
		System.out.println(jedis.get("starter"));
	}
}

结果:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值