自己手动写一个springboot-starter,完整版

废话不多说,直接开始。
step1: 开发一个starter需要哪些东西?
1、Configuration类:必须
Properties类:必须(用户动态的从配置文件读取配置)
Service类: 可以有不同定义
2、spring.factories 文件:必须,在resources/META-INF/spring.factories
3、pom的配置

setp2:代码
创建一个maven项目,pom的详细配置
distributionManagement标签的作用是打包上传私有maven仓库的,自己学习配置。


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.startersgroupId>
    <artifactId>demo-starterartifactId>
    <version>1.0.0version>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-startersartifactId>
        <version>2.2.6.RELEASEversion>
    parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
    dependencies>

	
project>

Configuration类我这里命名为DemoConfiguration

//标记为配置类
@Configuration
//启动配置属性
@EnableConfigurationProperties(DemoProperties.class)
//保证DemoService接口在
@ConditionalOnClass(DemoService.class)
@ConditionalOnProperty(prefix = "demo",value = "enabled",matchIfMissing = true)
public class DemoConfiguration {
     

    @Autowired
    private DemoProperties demoProperties;


    @Bean
    @ConditionalOnMissingBean(DemoService.class)
    public  DemoService demoService(){
     
        DemoService service = new DemoService();
        service.setName(demoProperties.getName());
        return service;
    }
}

Properties类定义

@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
     
    private static final String default_name = "csdn";
    private String name = default_name;
    public String getName() {
     
        return name;
    }
    public void setName(String name) {
     
        this.name = name;
    }
}```

DemoService

```java
public class DemoService {
     
    private String name ;
    public String hello(){
     
        return "hello:"+getName();
    }
    public String getName() {
     
        return name;
    }
    public void setName(String name) {
     
        this.name = name;
    }
}

spring.factories文件:定义位置:resources/META-INF/spring.factories
EnableAutoConfiguration后面的值根据自己的包路径做调整

org.springframework.boot.autoconfigure.EnableAutoConfiguration= \
  com.starters.demo.DemoConfiguration

step3:如何使用自定义start
1、可以手动将打好的jar安装到本地仓库
2、先将自定义starter的jar上传到私有maven仓库,从私有仓库下载
具体操作这里不多做说明

具体操作:创建一个springboot项目,通过pom下载自定义starter

<dependency>
   <groupId>com.startersgroupId>
    <artifactId>demo-starterartifactId>
    <version>1.0.0version>
dependency>

test类

public class DemoServiceTest extends StarterTestApplicationTests {
     
    @Autowired
    private DemoService demoService;
    @Test
    public void helloTest(){
     
        System.out.println(demoService.hello());
    }
}

springboot的yml配置

demo:
  name: baidu

运行结果:
在这里插入图片描述

你可能感兴趣的:(springboot,java,spring,boot)