customize spring-boot-starter

1. 先参考官方的写法

例如:spring-boot-starter-web

@Configuration 		//指定这个类是一个配置类
@ConditionalOnMissingBean@ConditionalOnClass
@ConditionalOnXXX 	//在指定条件成立的情况下 自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean 					//给容 器中添加组件

@ConfigurationPropertie 结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容 器中
   
自动配置类要能加载
将需要启动就加载的自动配置类,配置在 META-INF /spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\

2. 自定义Starterstarter :

1、这个场景需要使用到的依赖是什么?

2、如何编写自动配置

3、模式:

  1. 启动器(starter)启动器只用来做依赖导入,依赖专门来写一个自动配置模块,启动器依赖自动配置,别人只需要引入启动器 例如:spring-boot-starter-web 作用只是导入依赖,依赖里面有具体实现

  2. 官方命名规范:前缀 “spring-boot-starter-[模块名]” 例如:“spring-boot-starter-web”

    自定义命名规范:后缀 “-spring-boot-starter” 例如:“mybatis-spring-boot-starter”

实例步骤:

  1. 先建一个空项目,以Maven 空项目作为启动器 快速构建作为依赖配置
  2. 导入将要写的依赖坐标
    依赖管理
  3. 编写 依赖实现 (删掉主方法和测试方法,以及test和junit坐标)
public class HelloService {
    
    
   
    HelloProperties helloProperties;

    public String sayHello(String name){
    
    
        return helloProperties.getPrefix()+name+""+helloProperties.getSuffix();
    }
   
    public HelloProperties getHelloProperties() {
    
    
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
    
    
        this.helloProperties = helloProperties;
    }
}
@ConfigurationProperties(prefix = "whgc.hello") //对应属性前缀
public class HelloProperties {
    
    
    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;
    }
}
@Configuration
@ConditionalOnWebApplication //web 应用才生效
@EnableConfigurationProperties(HelloProperties.class)   //这样HelloProperties属性文件就生效了
public class HelloServiceAutoConfiguration {
    
    

    @Autowired
    HelloProperties helloProperties;
    @Bean
    public HelloService getHelloService(){
    
    
        HelloService hs = new HelloService();
        hs.setHelloProperties(helloProperties);
        return hs;
    }
}

详细视图 在这里插入图片描述
4. install 依赖 到 本地Maven (双击【install】)在这里插入图片描述
install 启动器 到本地 Maven在这里插入图片描述

使用自定义starter

  1. 引入启动器的坐标在这里插入图片描述

  2. 编写控制器及yaml文件

    @RestController
    public class HelloController {
          
          
        @Autowired
        HelloService helloService;
        @GetMapping("/hello")
        public String hello(){
          
          
            return helloService.sayHello("---hello---");
        }
    }
    
    whgc:
      hello:
        prefix: 你好
        suffix: Hello World
    
  3. 启动测试在这里插入图片描述
    不知道怎么滴每次 写hello world都异常的兴奋,完结。小白的一次进阶不容易

源文件

猜你喜欢

转载自blog.csdn.net/weixin_44961083/article/details/105837058