springboot-自定义starter

文章目录

  • starter命名规范
  • 工程目录结构
  • 实体类
  • service类
  • 配置类
  • 测试
  • 总结

starter命名规范

官方

  • 前缀:spring-boot-starter-xxx
  • 如:spring-boot-starter-web、spring-boot-starter-test

自定义

  • 前缀:xxx-spring-boot-starter
  • 如:mystarter-spring-boot-starter

工程目录结构

springboot-自定义starter_第1张图片

实体类

package com;

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

/**
 * @Component 将普通JavaBean实例化到spring容器
 * 相当于
 * @ConfigurationProperties(prefix = "tjw")
 * 配置属性、前缀以tjw开头
 */
@Component
@ConfigurationProperties(prefix = "tjw")
public class MyProperties {

    private String prefix;
    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }


}

service类

package com;

public class MyService {

    MyProperties myProperties;

    public MyProperties getMyProperties() {
        return myProperties;
    }

    public void setMyProperties(MyProperties myProperties) {
        this.myProperties = myProperties;
    }

    public String hello(String name){

      return myProperties.getPrefix()+name+myProperties.getSuffix();
  }
}

配置类

package com;

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

/**
 * @ConditionalOnWebApplication web环境下此配置类才生效
 * @EnableConfigurationProperties
 * 使ConfigurationProperties注解生效
 */
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(MyProperties.class)
public class MyServiceAutoConfiguration {

    @Autowired
    MyProperties myProperties;

    @Bean
    public MyService hello(){

        MyService myService = new MyService();
        myService.setMyProperties(myProperties);
        return myService;
    }
}

重点
spring.factories文件
MyServiceAutoConfiguration类的全路径

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.MyServiceAutoConfiguration

新建一个工程(启动器模块),在pom.xml文件中引入mystarter-autoconfigurare模块
启动器模块:启动器模块是一个空的jar文件,仅提供辅助性依赖管理,可用于指定装配或其他类库

   
        
            com.mystarter
            mystarterautoconfigurare
            0.0.1-SNAPSHOT
        
        

    
        UTF-8
    

然后把这两个项目打包 右上角的maven→→install
等待打包完成

测试

在测试项目中的pom.xml中引入

   
        
            com.tjw.starter
            mystarter-spring-boot-starter
            1.0-SNAPSHOT
        
package com;

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


@RestController
public class MyController {

    @Autowired  //注入MyService
    MyService myService;

    @GetMapping("/h")
    public String hello(){
     return myService.hello("tjw");
    }
}

这样就完成了自定义starter的创建、测试

总结

1、在mystarter-spring-boot-starter项目中添加mystarter-autoconfigurare依赖,然后在测试项目中添加mystarter-spring-boot-starter依赖

你可能感兴趣的:(学习笔记,spring,boot)