自定义spring-boot-starter

一般我们自定义spring-starter-xxx需要两个项目,一个是xxx-spring-boot-starter 和一个xxx-spring-boot-autoconfiguration

1.开始创建项目 首先 创建父工厂springboot父项目作为项目的依赖管理项目

img

2.这里我们选择类型为pom

img

3.一直下一步直至项目创建完成 父项目添加 pom

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modules>
<module>minghui-spring-boot-starter</module>
<module>minghui-spring-boot-autoconfiuration</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>minghui-demo</name>
<description>明慧自定义spring-boot-starter</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

4.创建子模块maven项目作为starter模块
5.创建子模块maven项目作为autoconfiguration模块 并在starter模块引入autoconfiguration模块

img

6.创建实体包配置包业务包

img

7. 创建实体类 对应读取相应的yml

package entity;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author ming
 * @create 2022/3/24
 * @description:
*/
@Component
@ConfigurationProperties(prefix = "minghui")
public class MingHuiProperties {
private String pet;
    private String petName;
    private String petAge;

    public MingHuiProperties(String pet, String petName, String petAge) {
this.pet = pet;
        this.petName = petName;
        this.petAge = petAge;
    }

public MingHuiProperties() {
    }

public String getPet() {
return pet;
    }

public void setPet(String pet) {
this.pet = pet;
    }

public String getPetName() {
return petName;
    }

public void setPetName(String petName) {
this.petName = petName;
    }

public String getPetAge() {
return petAge;
    }

public void setPetAge(String petAge) {
this.petAge = petAge;
    }
}
创建配置类
@Configuration
@EnableConfigurationProperties(MingHuiProperties.class)
@ConditionalOnProperty(prefix ="minghui",value = "pet")
public class MingHuiAutoConfiuration {
@Autowired
MingHuiProperties mingHuiProperties;
    @Bean
public PetService petService(){
return new PetService(mingHuiProperties.getPetName(),mingHuiProperties.getPetAge());
    }
}
public class HelloService {
    private String name;
    private String value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public HelloService(String name, String value) {
        this.name = name;
        this.value = value;
        System.out.println("HelloService启动成功!");
    }
    public void say(){
        System.out.println(name+"say:"+value);
    }
 
 
 
 
public class PetService {
    private String petName;
    private String petAge;

    public PetService(String petName, String petAge) {
        this.petName = petName;
        this.petAge = petAge;
    }

    public String getPetName() {
        return petName;
    }

    public void setPetName(String petName) {
        this.petName = petName;
    }

    public String getPetAge() {
        return petAge;
    }

    public void setPetAge(String petAge) {
        this.petAge = petAge;
    }
    public void show(){
        System.out.println("宠物的名称是"+petName);
        System.out.println("宠物的年龄是"+petAge);
    }

8.在minghui-spring-boot-starter的resouces目录添加META-INF中配置spring.facotories,此文件在spirngboot启动是会被自动扫描

  org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  config.MingHuiAutoConfiuration

9.在另一个项目引入start项目
img
10.首先不在yml配置相应的内容启动spirngboot启动类

@SpringBootApplication
public class Test {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(Test.class, args);
        PetService petService = (PetService) run.getBean("petService");
        petService.show();

    }
}

11.会发现报错因为在前面的配置类中加入了@ConditionalOnProperty(prefix ="minghui",value = "pet")即必须在yml文件中配置相应的内容
img
12.配置相应的内容后
img
13.正常启动了哈哈-啦啦--------
img

posted @ 2022-03-24 14:18  hmh_星星  阅读(84)  评论(0)    收藏  举报