浅谈一下Spring Boot Starter的实现

使用Spring cloud的时候,需要使用某些功能的时候,通过引用starter然后做些简单的配置就可以直接使用对应的功能,卧槽,爽,哎,那自己想自己撸一个要怎么实现呢?

官方文档有这么一段

pring Boot 检查已发布的 jar 中是否存在 META-INF/spring.factories 文件。该文件应在 EnableAutoConfiguration 键下列出您的配置类

“EnableAutoConfiguration 键”具体点应该是org.springframework.boot.autoconfigure.EnableAutoConfiguration

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.mycorp.libx.autoconfigure.LibXAutoConfiguration,
com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration

定义一个starter包含下面几个步骤:

1、创建自动装配类,Spring boot自动根据spring.factories扫描,可以理解为starter的入口类,多个类会spring.factories配置会按配置的顺序执行。

2、定义配置的承载属性类,用于承载配置文件的配置项

3、定义功能的实现类,在自动装配类里注入

先创建个Maven的项目

spring-boot-configuration-processor并不是必须的,这个在生成jar包之后,会产生一个spring-configuration-metadata.json元数据描述文件,在引用的时候,配置application.properties或者application.yaml的时候可以自动提示配置项。

    com.springredis.starter
   
    spring-boot-starter-springredis
    1.0.1
    
    jar
    
        spring-boot-starter-parent
        org.springframework.boot
        2.7.0
    
    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
    

定义自动装配的类

package com.springredis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(SpringRedisProperty.class)
@ConditionalOnProperty(prefix = "springredis",value = "enabled",matchIfMissing = true)
public class SpringRedisAutoConfiguration {

    /*
    * 把SpringRedisProperty注入进来,传递给SpringRedisService使用
    * */
    @Autowired
    SpringRedisProperty springRedisProperty;

    /*
    * 把SpringRedisService自动装配到容器
    * */
    @Bean
    public  SpringRedisService springRedisService(){
        return  new SpringRedisService(springRedisProperty);
    }
}

定义配置承载类,读取redis的账号密码服务器ip端口和db的下表

package com.springredis;

import org.springframework.boot.context.properties.ConfigurationProperties;
    /*
    * 以springredis为前缀,配置的时候用springredis.xxxx为key
    * */
@ConfigurationProperties(prefix = "springredis")
public class SpringRedisProperty {
    private  String host;
    private  Integer port;
    private  Integer dbIndex;
    private  String userName;
    private  String password;

    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 Integer getDbIndex() {
        return dbIndex;
    }

    public void setDbIndex(Integer dbIndex) {
        this.dbIndex = dbIndex;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

定义功能实现类,根据配置(SpringRedisProperty类承载了读取配置文件的值),拼接一个redis的字符串,打印一下日志。

package com.springredis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SpringRedisService {

    private SpringRedisProperty springRedisProperty;
    private Logger logger=LoggerFactory.getLogger(SpringRedisService.class);
    public  SpringRedisService(SpringRedisProperty springRedisProperty){
        this.springRedisProperty=springRedisProperty;
        getRedisConnectionString();
    }


    public  String getRedisConnectionString(){
        String connectionString =
                  this.springRedisProperty.getUserName()+":"
                + this.springRedisProperty.getPassword()+":"
                + this.springRedisProperty.getHost()
                +"@"+this.springRedisProperty.getPort()
                +"/"+this.springRedisProperty.getDbIndex();
        logger.info(connectionString);
        return  connectionString;
    }
}

在resource创建一个META-INF创建一个spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.springredis.SpringRedisAutoConfiguration

最后生成jar,测试环境就可以开始了。

创建个spring-boot-web

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.springredis
            system
            spring-boot-starter-springredis
            1.0.1
            ${project.basedir}/lib/spring-boot-starter-springredis-1.0.1.jar
        
    


把生成的starter的jar拷贝到lib目录下(这个文件夹随便定义,在pom对应写上正确的引用路径即可)。

添加starter的配置信息

application.yaml

springredis:
  host: 127.0.0.1
  port: 6379
  user-name: aming
  password: 123456
  db-index: 0

其他的业务代码都不写,启动测试一下。


定义个Controller调用一下Service的接口

package com.starter.startertest;

import com.springredis.SpringRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StarterController {

    @Autowired
    SpringRedisService service;

    @GetMapping("test")
    public  String GetRedisConnectionString(){
        return service.getRedisConnectionString();
    }
}


nice!!!

展开阅读全文

页面更新:2024-04-21

标签:前缀   字符串   端口   容器   文件夹   路径   账号   定义   功能   文件

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top