• 自定义springboot-start


    自定义springboot-start

    1.创建空项目
    2.创建子模块xxx-spring-boot-starter
    3.创建子模块xxx-spring-boot-autoconfigure
    4.在自定义的starter pom中引入autoconfigure依赖
    6.在autoconfigure中编写具体的configerProperties类与autoConfigure类
    7.完成后创建META-INF/spring.factories文件,引入自动配置类
    8.编写完成项目后对两个模块依次打包
    9.创建新项目,引入自定义starter测试
    例:
    • 自定义entity
    public class WebDriver {
        private String version;
    
        public void setVersion(String version) {
            this.version = version;
        }
    
        public String info() {
            return "version:" + version;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 自定义ConfigProperties
    package org.springframework.config;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "spring.selenium")
    public class DriverConfigProperties {
        private String desc;
    
        public String getDesc() {
            return desc;
        }
    
        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 自定义AutoConfigure类
    package org.springframework.config;
    
    import org.springframework.Driver;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @EnableConfigurationProperties(DriverConfigProperties.class)
    @Configuration
    public class DriverAutoConfigure {
        @ConditionalOnMissingBean(Driver.class)
        @Bean
        public Driver driver(DriverConfigProperties properties){
            Driver driver = new Driver();
            driver.setDescription(properties.getDesc());
            return driver;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 创建META-INF/spring.factories并配置
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    org.springframework.config.DriverAutoConfigure
    
    • 1
    • 2
    • 打包项目
    • 在starter项目中引入自动配置依赖
    <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>selenium-spring-boot-autoconfigureartifactId>
                <version>0.0.1-SNAPSHOTversion>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 打包starter
  • 相关阅读:
    SpringBoot MongoDB GridFsTemplate实现文件管理
    StrongSORT(deepsort强化版)浅实战+代码解析
    “SonarQube requires Java 11+ to run“ for java 1.8.0_221
    英语——名言篇——成语
    一个数据库版本兼容问题
    图文加多个测试带你彻底搞懂Netty ChannelPipeline的执行顺序(附源码)
    优化理论20---插值法: Hermite插值法、龙格现象、分段插值、样条插值
    中国贵金属白银技巧:与做人
    C#的Winform窗体淡出关闭效果
    Flask 学习-4.templates 渲染模板
  • 原文地址:https://blog.csdn.net/qq_52751442/article/details/127592578