• 自定义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
  • 相关阅读:
    蓝桥杯每日一题2023.10.20
    “高级Java注解的简介及应用“
    千亿级大数据如何存储的?
    Spring注解驱动之BeanPostProcessor后置处理器详解
    数据结构--插入排序
    循环神经网络介绍(RNN)
    <数据结构> - 数据结构在算法比赛中的应用(下)
    【六袆 - MySQL】SQL优化;Explain SQL执行计划分析;
    基于MS16F3211芯片的触摸控制灯的状态变化和亮度控制总结版(11.22)
    必应Bing国内直投和第三方合作流量有什么区别?
  • 原文地址:https://blog.csdn.net/qq_52751442/article/details/127592578