Spring Boot(十三):自定义starter启动器

1 篇文章 0 订阅
1 篇文章 0 订阅

一、简介

SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应的功能。即使是这样,springboot也不能囊括我们所有的使用场景,往往我们需要自定义starter,来简化我们对springboot的使用。

二、自定义starter

 starter命名规则:官方名称:spring-boot-starter-xxx   第三方命名:xxx-spring-boot-starter

 1. 创建项目

       我们需要先创建一个项目命名:redis-spring-boot-starter

 2. 引入相关依赖

       自动配置相关的依赖:          

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

   redis相关依赖:

<dependency>
	<groupId>org.springframework.data</groupId>
	<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
</dependency>

  3. 编写redis配置类

          在使用Spring官方的Starter时通常可以在application.properties中来配置参数覆盖掉默认的值,例如在使用redis时一般就会有对应的RedisProperties     

@ConfigurationProperties(prefix="redis")
public class RedisProperties {
	
	private String host = "127.0.0.1";
	private Integer port = 6379;
	private String password = "";
	private Config config = new Config();
	
	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;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Config getConfig() {
		return config;
	}
	public void setConfig(Config config) {
		this.config = config;
	}
	@Override
	public String toString() {
		return "RedisProperties [host=" + host + ", port=" + port + ", password=" + password + ", config=" + config
				+ "]";
	}
}

       

public class Config {
	private Integer maxIdle = 50;
	private Integer maxTotal = 100;
	private Integer timeout = 5000;
	private Integer maxWaitMillis = 3000;
	private Boolean testOnBorrow = true;
	public Integer getMaxIdle() {
		return maxIdle;
	}
	public void setMaxIdle(Integer maxIdle) {
		this.maxIdle = maxIdle;
	}
	public Integer getMaxTotal() {
		return maxTotal;
	}
	public void setMaxTotal(Integer maxTotal) {
		this.maxTotal = maxTotal;
	}
	public Integer getTimeout() {
		return timeout;
	}
	public void setTimeout(Integer timeout) {
		this.timeout = timeout;
	}
	public Integer getMaxWaitMillis() {
		return maxWaitMillis;
	}
	public void setMaxWaitMillis(Integer maxWaitMillis) {
		this.maxWaitMillis = maxWaitMillis;
	}
	public Boolean getTestOnBorrow() {
		return testOnBorrow;
	}
	public void setTestOnBorrow(Boolean testOnBorrow) {
		this.testOnBorrow = testOnBorrow;
	}
	@Override
	public String toString() {
		return "Config [maxIdle=" + maxIdle + ", maxTotal=" + maxTotal + ", timeout=" + timeout + ", maxWaitMillis="
				+ maxWaitMillis + ", testOnBorrow=" + testOnBorrow + "]";
	}
}

 4. 自动配置类

      一般每个starter都至少会有一个自动配置类,一般命名规则使用XxxAutoConfiguration, 例如RedisAutoConfiguration          

@SpringBootConfiguration
@EnableConfigurationProperties(RedisProperties.class)
@ConditionalOnClass(Jedis.class)
public class RedisAutoConfiguration {

	@Autowired
	private RedisProperties redisProperties;

	@Bean
	public RedisConnectionFactory createRedisConnectionFactory() {
		JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
		connectionFactory.setHostName(redisProperties.getHost());
		connectionFactory.setPort(redisProperties.getPort());
		connectionFactory.setPassword(redisProperties.getPassword());
		connectionFactory.setTimeout(redisProperties.getConfig().getTimeout());
		connectionFactory.getPoolConfig().setMaxIdle(redisProperties.getConfig().getMaxIdle());
		connectionFactory.getPoolConfig().setMaxTotal(redisProperties.getConfig().getMaxTotal());
		connectionFactory.getPoolConfig().setMaxWaitMillis(redisProperties.getConfig().getMaxWaitMillis());
		connectionFactory.getPoolConfig().setTestOnBorrow(redisProperties.getConfig().getTestOnBorrow());
		return connectionFactory;
	}

	@Bean
	public RedisTemplate taskRedisTemplate() {
		RedisTemplate template = new StringRedisTemplate();
		template.setKeySerializer(new StringRedisSerializer());
		template.setValueSerializer(new JdkSerializationRedisSerializer());
		template.setHashKeySerializer(new StringRedisSerializer());
		template.setHashValueSerializer(new JdkSerializationRedisSerializer());
		template.setConnectionFactory(createRedisConnectionFactory());
		return template;
	}
}

5.spring.factories

    在 resources 下创建文件夹 META-INF 并在 META-INF 下创建文件 spring.factories ,在该文件中配置自己的自动配置类。

    目录结构:

      

    spring.factories文件内容

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.reyco.redis.core.autoConfiguration.RedisAutoConfiguration

   到这儿,我们的配置自定义的starter就写完了...

三. 测试自定义starter

   1. 创建测试项目,引入自定义redis依赖       

<!-- 自定义redis start -->
<dependency>
	<groupId>com.reyco.redis</groupId>
	<artifactId>redis-spring-boot-starter</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>

    2.  配置application.properties           

redis.host=127.0.0.1
redis.port=6379
redis.password=123456
redis.config.maxIdle=50
redis.config.maxTotal=100
redis.config.timeout=5000
redis.config.maxWaitMillis=5000
redis.config.testOnBorrow=true

     3. 测试controller  

@RestController
public class TestRedisController {
	
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	@RequestMapping(value = "/test/redis")
	public String redis() {
		ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
		opsForValue.set("name", "admin");
		String name = opsForValue.get("name");
		System.out.println("name="+name);
		return "name="+name;
	}
}

             效果:        

       starter引用成功。。。。。。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java的艺术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值