SpringBoot核心——starter实现

  • 首先创建一个springboot项目
  • pom.xml导入依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
  • 删除<build>标签所有内容
  • 指定工程版本
  •  
    • 包结构:

    • DemoAutoConfiguration:自动配置类
    • Demoproperties:配置文件
    • DemoService:Demo测试对象
    • META-INF/spring.factories(需要自己创建):springboot启动时会从这个文件找自动配置类
  • 实现:
    • DemoProperties
package org.mystarter.properties;

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

/**
 * 可在 .Properties 配置的属性
 */
@ConfigurationProperties(prefix = "spring.demo")//配置文件前缀
public class DemoProperties {


    private String value ;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
  •  DemoService        
    package org.mystarter.service;
    
    import org.mystarter.properties.DemoProperties;
    
    public class DemoService {
    
        public DemoProperties demoProperties;
    
        /**
         *  返回 properties 的配置属性值
         * @return
         */
        public String getProperties(){
        return demoProperties.getValue();
    }
    
    
    }
  •  DemoAutoConfiguration
package org.mystarter.autoconfiguration;

import org.mystarter.properties.DemoProperties;
import org.mystarter.service.DemoService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({DemoProperties.class}) // 把DemoProperties配置文件加载进来
public class DemoAutoConfiguration {

    /**
     * 将demoService 放入IOC容器中
     * @param demoProperties properties配置文件
     * @return  demoService对象
     */
    @Bean
    DemoService MyService(DemoProperties demoProperties){
        DemoService demoService = new DemoService();
        demoService.demoProperties = demoProperties;
        return demoService;
    }


}
  • META-INF/spring.factories
#springboot启动时会扫 META-INF/spring.factoties 文件中 KEY为 org.springframework.boot.autoconfigure.EnableAutoConfiguration
# 的配置文件,实现自动配置 (Mybatis也是通过这个实现自动配置)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  org.mystarter.autoconfiguration.DemoAutoConfiguration
# Value 是自己的Configuration 路径
  • 最后打包
    • 通过命令打包
      cxg@debian:~/IdeaProjects/demo-spring-boot-starter$     mvn clean package
      
    • 通过自带打包操作

      打包好的starter(jar)

      放入你的maven仓库中就可以被其他项目使用

  • 使用(test)

    • 创建一个springboot项目

    • pom.xml引入刚打包的starter

              <dependency>
                  <groupId>org.mystarter</groupId>
                  <artifactId>demo-spring-boot-starter</artifactId>
                  <version>1.0.0</version>
              </dependency>
      

      application.properties

      # 应用名称
      spring.application.name=mystartertest
      # 应用服务 WEB 访问端口
      server.port=8080
      
      #自定义starter的属性配置
      spring.demo.value=myStarter
      

      编写controller测试

      package com.example.mystartertest.controller;
      
      import org.mystarter.service.DemoService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      /**
       * 测试controller
       */
      @RestController
      @RequestMapping("demos")
      public class DemoController {
      
          @Autowired
          DemoService service;
      
          /**
           * 使用自动注入的DemoService对象使用其getProperties方法
           * @return   配置文件spring.demo.value的值
           */
          @GetMapping
          public String getDemoValue(){
              return service.getProperties();
          }
      
      }

      测试接口:http://localhost:8080/demos

到这里springboot自定义starter就结束了

starter源码:https://gitee.com/feiliangren/demo-starter.git

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值