栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot自定义Starter

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot自定义Starter

SpringBoot自定义Starter 如何实现自定义Starter 先创建一个父工程

我们会把自定义starter放到这个父工程里面,然后建立一个子工程去引入那个自定义starter

自定义Starter的命名规范
  • 官方命名空间

    • 前缀:spring-boot-starter-

    • 模式:spring-boot-starter-模块名

    • 举例:spring-boot-starter-web、spring-boot-starter-jdbc

  • 自定义命名空间

    后缀:-spring-boot-starter

    模式:模块-spring-boot-starter

    举例:mybatis-spring-boot-starter

开始创建自定义Starter

引入依赖


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
    
    linc.fun
    linc-spring-boot-starter
    1.0-SNAPSHOT

    
        2.6.4
    
    
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
        
            org.springframework.boot
            spring-boot-starter-web
            ${spring-boot.version}
        
    
    
        
            springsource-repos
            SpringSource Repository
            http://repo.spring.io/release/
        
        
            activiti-repos2
            Activiti Repository 2
            https://app.camunda.com/nexus/content/groups/public
        
        
            central-repos
            Central Repository
            http://repo.maven.apache.org/maven2
        
    

我们自定义一个配置文件
@ConfigurationProperties("linc.properties")
public class MyProperties {
    @Value("${linc.properties.name}")
    private  String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
自定义一个Controller
@RestController
public class MyController {
    @Resource
    private MyProperties myProperties;
  
    @RequestMapping("/my")
    public String my() {
        System.out.println("my....");
        return myProperties.getName() + ",正在测试...";
    }
}
定义一个自动配置类
@Configuration
@ConditionalOnProperty(value = "linc.properties.name")
@EnableConfigurationProperties(MyProperties.class)
public class MyAutoConfiguration {

}
在resources目录下创建META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
  linc.fun.MyAutoConfiguration

到这儿,我们的配置自定义的starter就写完了

我们进行打包

创建另外一个子项目,引入我们这个自定义Starter

引入依赖


    4.0.0

    linc.fun
    linc-project
    1.0-SNAPSHOT

    
        8
        8
    
    
        
            linc.fun
            linc-spring-boot-starter
            1.0-SNAPSHOT
        
    

创建启动类以及配置文件填写

启动类

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

application.yml

server:
  port: 8080

linc:
  properties:
    name: linc
访问 http://localhost:8080/my

注意我们配置了@ConditionalOnProperty(value = “linc.properties.name”)

所以的在application.yml配置了这个配置信息才能成功启动

代码下载地址

下载地址: https://linq-cool.oss-cn-shanghai.aliyuncs.com/20220426/eaedc35d680f467c8ecab516bc332ff7.zip

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/837600.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号