springboot自定义starter组件

源于蚂蚁课堂的学习,点击这里查看

以仿真从redis获取token为例

1.新建maven工程

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <dependencies>
        <!-- 自定义springboot的组件,必须引入此依赖作为入口 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>

        <!-- 读取配置文件时的自动提示功能 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

2.读取配置的工具类

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

/**
 * @Description todo
 * @Author: yanxh<br>
 * @Date 2020-04-09 13:50<br>
 * @Version 1.0<br>
 */
@ConfigurationProperties(prefix = "yanxiaohui")
public class TokenProperties {

    private String tokenRedisUser;

    private String tokenRedisPassword;

    public String getTokenRedisUser() {
        return tokenRedisUser;
    }

    public void setTokenRedisUser(String tokenRedisUser) {
        this.tokenRedisUser = tokenRedisUser;
    }

    public String getTokenRedisPassword() {
        return tokenRedisPassword;
    }

    public void setTokenRedisPassword(String tokenRedisPassword) {
        this.tokenRedisPassword = tokenRedisPassword;
    }
}

3.定义配置类 

import live.yanxiaohui.utils.TokenProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description todo
 * @Author: yanxh<br>
 * @Date 2020-04-09 14:00<br>
 * @Version 1.0<br>
 */
@Configuration
@EnableConfigurationProperties(TokenProperties.class)
public class TokenConfig {

}

4.定义具体的API业务功能

import live.yanxiaohui.utils.TokenProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description todo
 * @Author: yanxh<br>
 * @Date 2020-04-09 14:05<br>
 * @Version 1.0<br>
 */
@Service
public class TokenService {

    @Autowired
    private TokenProperties tokenProperties;

    public String getToken() {
        return "user=[" + tokenProperties.getTokenRedisUser() + "],password=[" + tokenProperties.getTokenRedisPassword() + "]";
    }
}

 5.定义程序的入口(核心)

 

需在resources目录下按照约定建立一下路径的文件
/MEDA-INF/spring.factories

并配置其入口参数
org.springframework.boot.autoconfigure.EnableAutoConfiguration=live.yanxiaohui.config.TokenConfig

参数值为组件的配置类,即@Configuration的使用者

主要用于springboot启动时去加载starter组件中的配置

 6.maven clean install

将maven工程编译放入maven仓库

 7.新增springboot工程

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
  </parent>

  <dependencies>
      <!-- 自定义的springboot stater组件 -->
      <dependency>
          <groupId>live.yanxiaohui</groupId>
          <artifactId>token-spring-boot-starter</artifactId>
          <version>1.0-SNAPSHOT</version>
      </dependency>

        
      <!-- 整合springmvc-->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
          <version>2.0.0.RELEASE</version>
      </dependency>

  </dependencies>

 8.完善配置文件

9.测试 

import live.yanxiaohui.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description todo
 * @Author: yanxh<br>
 * @Date 2020-04-09 14:20<br>
 * @Version 1.0<br>
 */
@RestController
public class IndexController {

    @Autowired
    private TokenService tokenService;

    @RequestMapping("/")
    public String index(){
        return tokenService.getToken();
    }
}

10.总结 

1.springboot的starter组件,其实就是将某些第三方框架的API功能进行提前的整合,通过maven依赖的功能将其延用至实际使用中
2.自定义springboot的starter组件,需要按照规范建立对应的配置文件入口

springboot的设计理念:
1.通过maven的依赖和springboot自定义的starter组件,可以快速整合第三方常用框架,省去手动配置开销
2.通过对spring体系注解的使用和包装,去除了XML配置,简化代码
3.java手动创建tomcat服务器,将本地的Class文件交由tomcat管理
原创文章 148 获赞 258 访问量 11万+

猜你喜欢

转载自blog.csdn.net/yxh13521338301/article/details/105409930
今日推荐