您当前的位置: 首页 >  ar

white camel

暂无认证

  • 1浏览

    0关注

    442博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringBoot——自定义starter分析

white camel 发布时间:2020-04-28 09:36:11 ,浏览量:1

一、自定义starter
  • 启动器只用来做依赖导入

  • 专门来写一个自动配置模块;

  • 启动器依赖自动配置模块,项目中只需要引入相应的starter就会引入启动器的所有传递依赖

在这里插入图片描述

也就是说xxx-starter(启动器)依赖于xxxx-starter-autoconfigurer(自动配置), 别人要使用,就依赖xxx-starter启动器,就自动引入了自动配置。

1、启动器

启动器模块是一个空JAR文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库。

2、命名规约
  • 官方命名

    spring-boot-starter-模块名

    eg:spring-boot-starter-web、spring-boot-starter-jdbc、spring-boot-starter-thymeleaf

  • 自定义命名

    模块名-spring-boot-starter

    eg: mybatis-spring-boot-start

3、如何编写自动配置
@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中
public class XxxxAutoConfiguration {

自动配置类要能加载,需要将启动就加载的自动配置类配置在META-INF/spring.factories

二、案例 (自定义启动器)
  • 首先创建一个项目, 这个项目中创建两个模块; 一个模块是一个Maven模块(启动器), 一个模块是一个SpringBoot模块(自动配置模块) 在这里插入图片描述
1、自动配置模块
  • 1.创建一个自动配置模块,和创建普通springboot项目一样,不需要引入其他starter
  • 删除掉多余的文件和依赖

pom文件



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.2.6.RELEASE
		 
	
	com.zy.starter
	zy-spring-boot-starter-autoconfigurer
	0.0.1-SNAPSHOT
	zy-spring-boot-starter-autoconfigurer
	Demo project for Spring Boot

	
		1.8
	

	
		
		
			org.springframework.boot
			spring-boot-starter
		

		
		
			org.springframework.boot
			spring-boot-configuration-processor
			true
		
	


  • 2.创建配置类和自动配置类

配置类

/**
 * Description:
 *
 * @author zygui
 * @date 2020/4/19 11:00
 */
@ConfigurationProperties(prefix = "gzy")
public class GzyProperties {

    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;
    }
}

自动配置类

/**
 * Description: 自动配置类
 *
 * @author zygui
 * @date 2020/4/19 11:05
 */
@Configuration
@ConditionalOnWebApplication // web应用才生效
@EnableConfigurationProperties(GzyProperties.class) //让配置类生效,(注入到容器中)
public class GzyAutoConfiguration {

    @Autowired
    private GzyProperties gzyProperties;

    @Bean
    public HelloService helloService() {
        HelloService service = new HelloService();
        service.setGzyProperties(gzyProperties);
        return service;
    }
}

HelloService类

public class HelloService {

    GzyProperties gzyProperties;

    public GzyProperties getGzyProperties() {
        return gzyProperties;
    }

    public void setGzyProperties(GzyProperties gzyProperties) {
        this.gzyProperties = gzyProperties;
    }

    public String sayHelloGzy(String name) {
        return gzyProperties.getPrefix() + "-" + name + gzyProperties.getSuffix();
    }
}

这里也可以省略HelloService, 将其作为自动配置类的一个内部类, SpringBoot源码大多也是这样写的; 具体看下面 配置类不变

自动配置类

/**
 * Description: 自动配置类
 *
 * @author zygui
 * @date 2020/4/19 11:05
 */
@Configuration
@ConditionalOnWebApplication // web应用才生效
@EnableConfigurationProperties(GzyProperties.class) //让配置类生效,(注入到容器中)
public class GzyAutoConfiguration {

    private final GzyProperties gzyProperties;

    /**
     * 通过构造器注入
     *
     * @param gzyProperties
     */
    public GzyAutoConfiguration(GzyProperties gzyProperties) {
        this.gzyProperties = gzyProperties;
    }

    @Bean
    public HelloService helloService() {
        return new HelloService();
    }

    public class HelloService {

        public String sayHelloGzy(String name) {
            return gzyProperties.getPrefix() + "-" + name + "-" + gzyProperties.getSuffix();
        }
    }
}
  • 3.在resources文件夹下创建META-INF/spring.factories 在这里插入图片描述

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.guizy.spring.boot.autoconfigure.GzyAutoConfiguration
  • 4.安装到本地仓库 在这里插入图片描述
2、创建自定义启动器

5.创建starter,选择maven工程即可,只是用于管理依赖,添加对AutoConfiguration模块的依赖



    4.0.0

    com.zy
    zy-spring-boot-starter
    1.0-SNAPSHOT

    
    
        
        
            com.zy.starter
            zy-spring-boot-starter-autoconfigurer
            0.0.1-SNAPSHOT
        
    


  • 6.安装到本地仓库 在这里插入图片描述
3、创建项目测试,选择添加web场景,因为设置是web场景才生效

7.引入自定义的启动器



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.6.RELEASE
         
    
    com.zy
    springboot-09-starter-test
    0.0.1-SNAPSHOT
    springboot-09-starter-test
    Demo project for Spring Boot

    
        1.8
    

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

        
        
            com.zy
            zy-spring-boot-starter
            1.0-SNAPSHOT
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


在这里插入图片描述

  • 8.创建Controller
@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(){
        return helloService.sayHelloGzy("GZY");
    }
}
  • 9.在application.properties配置文件中可以配置
gzy.prefix=HELLO
gzy.suffix=WORLD

也可以搜索GzyProperties可以看到自定义XxxProperties

  • 10.测试 在这里插入图片描述

调用过程 : 在这里插入图片描述

关注
打赏
1661428283
查看更多评论
立即登录/注册

微信扫码登录

0.0400s