当前位置:   article > 正文

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

springboot启动器

一、简介

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

二、自定义starter

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

 1. 创建项目

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

 2. 引入相关依赖

       自动配置相关的依赖:          

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-autoconfigure</artifactId>
  4. </dependency>

   redis相关依赖:

  1. <dependency>
  2. <groupId>org.springframework.data</groupId>
  3. <artifactId>spring-data-redis</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>redis.clients</groupId>
  7. <artifactId>jedis</artifactId>
  8. </dependency>

  3. 编写redis配置类

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

  1. @ConfigurationProperties(prefix="redis")
  2. public class RedisProperties {
  3. private String host = "127.0.0.1";
  4. private Integer port = 6379;
  5. private String password = "";
  6. private Config config = new Config();
  7. public String getHost() {
  8. return host;
  9. }
  10. public void setHost(String host) {
  11. this.host = host;
  12. }
  13. public Integer getPort() {
  14. return port;
  15. }
  16. public void setPort(Integer port) {
  17. this.port = port;
  18. }
  19. public String getPassword() {
  20. return password;
  21. }
  22. public void setPassword(String password) {
  23. this.password = password;
  24. }
  25. public Config getConfig() {
  26. return config;
  27. }
  28. public void setConfig(Config config) {
  29. this.config = config;
  30. }
  31. @Override
  32. public String toString() {
  33. return "RedisProperties [host=" + host + ", port=" + port + ", password=" + password + ", config=" + config
  34. + "]";
  35. }
  36. }

       

  1. public class Config {
  2. private Integer maxIdle = 50;
  3. private Integer maxTotal = 100;
  4. private Integer timeout = 5000;
  5. private Integer maxWaitMillis = 3000;
  6. private Boolean testOnBorrow = true;
  7. public Integer getMaxIdle() {
  8. return maxIdle;
  9. }
  10. public void setMaxIdle(Integer maxIdle) {
  11. this.maxIdle = maxIdle;
  12. }
  13. public Integer getMaxTotal() {
  14. return maxTotal;
  15. }
  16. public void setMaxTotal(Integer maxTotal) {
  17. this.maxTotal = maxTotal;
  18. }
  19. public Integer getTimeout() {
  20. return timeout;
  21. }
  22. public void setTimeout(Integer timeout) {
  23. this.timeout = timeout;
  24. }
  25. public Integer getMaxWaitMillis() {
  26. return maxWaitMillis;
  27. }
  28. public void setMaxWaitMillis(Integer maxWaitMillis) {
  29. this.maxWaitMillis = maxWaitMillis;
  30. }
  31. public Boolean getTestOnBorrow() {
  32. return testOnBorrow;
  33. }
  34. public void setTestOnBorrow(Boolean testOnBorrow) {
  35. this.testOnBorrow = testOnBorrow;
  36. }
  37. @Override
  38. public String toString() {
  39. return "Config [maxIdle=" + maxIdle + ", maxTotal=" + maxTotal + ", timeout=" + timeout + ", maxWaitMillis="
  40. + maxWaitMillis + ", testOnBorrow=" + testOnBorrow + "]";
  41. }
  42. }

 4. 自动配置类

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

  1. @SpringBootConfiguration
  2. @EnableConfigurationProperties(RedisProperties.class)
  3. @ConditionalOnClass(Jedis.class)
  4. public class RedisAutoConfiguration {
  5. @Autowired
  6. private RedisProperties redisProperties;
  7. @Bean
  8. public RedisConnectionFactory createRedisConnectionFactory() {
  9. JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
  10. connectionFactory.setHostName(redisProperties.getHost());
  11. connectionFactory.setPort(redisProperties.getPort());
  12. connectionFactory.setPassword(redisProperties.getPassword());
  13. connectionFactory.setTimeout(redisProperties.getConfig().getTimeout());
  14. connectionFactory.getPoolConfig().setMaxIdle(redisProperties.getConfig().getMaxIdle());
  15. connectionFactory.getPoolConfig().setMaxTotal(redisProperties.getConfig().getMaxTotal());
  16. connectionFactory.getPoolConfig().setMaxWaitMillis(redisProperties.getConfig().getMaxWaitMillis());
  17. connectionFactory.getPoolConfig().setTestOnBorrow(redisProperties.getConfig().getTestOnBorrow());
  18. return connectionFactory;
  19. }
  20. @Bean
  21. public RedisTemplate taskRedisTemplate() {
  22. RedisTemplate template = new StringRedisTemplate();
  23. template.setKeySerializer(new StringRedisSerializer());
  24. template.setValueSerializer(new JdkSerializationRedisSerializer());
  25. template.setHashKeySerializer(new StringRedisSerializer());
  26. template.setHashValueSerializer(new JdkSerializationRedisSerializer());
  27. template.setConnectionFactory(createRedisConnectionFactory());
  28. return template;
  29. }
  30. }

5.spring.factories

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

    目录结构:

      

    spring.factories文件内容

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

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

三. 测试自定义starter

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

  1. <!-- 自定义redis start -->
  2. <dependency>
  3. <groupId>com.reyco.redis</groupId>
  4. <artifactId>redis-spring-boot-starter</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. </dependency>

    2.  配置application.properties           

  1. redis.host=127.0.0.1
  2. redis.port=6379
  3. redis.password=123456
  4. redis.config.maxIdle=50
  5. redis.config.maxTotal=100
  6. redis.config.timeout=5000
  7. redis.config.maxWaitMillis=5000
  8. redis.config.testOnBorrow=true

     3. 测试controller  

  1. @RestController
  2. public class TestRedisController {
  3. @Autowired
  4. private RedisTemplate<String, String> redisTemplate;
  5. @RequestMapping(value = "/test/redis")
  6. public String redis() {
  7. ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
  8. opsForValue.set("name", "admin");
  9. String name = opsForValue.get("name");
  10. System.out.println("name="+name);
  11. return "name="+name;
  12. }
  13. }

             效果:        

       starter引用成功。。。。。。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/947759
推荐阅读
相关标签
  

闽ICP备14008679号