springboot 自定义starter实现

发布于:2022-10-15 ⋅ 阅读:(409) ⋅ 点赞:(0)

SpringBoot自动配置-自定义starter实现

自定义starter的实现通俗来说就是自定义一个依赖或场景
在自定义starter之前,需要了解以下注解:
  • @Configuration:标注的类为配置类
  • @EnableConfigurationProperties:该注解是用来开启配置参数的注解的,将配置文件中的变量加载到Properties的配置类对象当中,并注入到容器当中,结合@ConfigurationProperties 运用
  • @ConfigurationProperties:该注解使用必须将对象注入到IOC容器中才有配置绑定的功能。可以将application.properties配置中的值注入到bean对象上。
    并且SpringBoot还提供了一些@Conditional注解:
  • @ConditionalOnBean:当SpringIoc容器内存在指定Bean的条件
  • @ConditionalOnClass:当SpringIoc容器内存在指定Class的条件
  • @ConditionalOnExpression:基于SpEL表达式作为判断条件
  • @ConditionalOnJava:基于JVM版本作为判断条件
  • @ConditionalOnMissingBean:当SpringIoc容器内不存在指定Bean的条件
  • @ConditionalOnMissingClass:当SpringIoc容器内不存在指定Class的条件
  • @ConditionalOnNotWebApplication:当前项目不是Web项目的条件
  • @ConditionalOnProperty:指定的属性是否有指定的值
  • @ConditionalOnResource:类路径是否有指定的值
  • @ConditionalOnSingleCandidate:当指定Bean在SpringIoc容器内只有一个,或者虽然有多个但是指定首选的Bean
  • @ConditionalOnWebApplication:当前项目是Web项目的条件以上注解都是元注解 @Conditional演变而来的,根据不用的条件对应创建以上的具体条件注解。

自定义starter

步骤一:

新建一个 Empty Project 空项目
在这里插入图片描述

步骤二:

在空项目中添加一个maven项目和一个spring boot项目
在这里插入图片描述
在建设spring boot项目是需要注意的是,勾选场景步骤,不用勾选,直接下一步
在这里插入图片描述
maven项目为starter,springboot项目为starter自动配置
把两个项目建设完成后,删除spring boot项目没必要的部分:

  1. test文件
  2. 主程序类
  3. 默认配置文件 application.properties
步骤三:

spring boot项目的pom.xml配置文件:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lin</groupId>
    <artifactId>lin-hello-spring-boot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>lin-hello-spring-boot-starter-autoconfigure</name>
    <description>lin-hello-spring-boot-starter-autoconfigure</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
</project>

把spring boot项目的下面部分
在这里插入图片描述

加到maven项目的pom.xml配置文件中,
maven项目的pom.xml配置文件:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lin</groupId>
    <artifactId>lin-hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

<!--        starter自动配置-->
        <dependency>
            <groupId>com.lin</groupId>
            <artifactId>lin-hello-spring-boot-starter-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>
</project>

至此,maven项目不用再做如何添加修改

步骤四:

autoconfigure包中配置使用 META-INF/spring.factories 中 EnableAutoConfiguration 的值,使得项目启动加载指定的自动配置类
在这里插入图片描述
spring.factories:

# Auto Configure
# 使得项目启动加载指定的自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lin.config.helloconfig

com.lin.config.helloconfig 为配置类路径

步骤五:

在spring boot项目中加入内容:
在这里插入图片描述
Helloproperties.java:

@ConfigurationProperties("lin.hello")
public class Helloproperties {

    private String qian;
    private String hou;

    public String getQian() {
        return qian;
    }

    public void setQian(String qian) {
        this.qian = qian;
    }

    public String getHou() {
        return hou;
    }

    public void setHou(String hou) {
        this.hou = hou;
    }

}

helloconfig.java:

@Configuration
@EnableConfigurationProperties(Helloproperties.class)
public class helloconfig {

    @ConditionalOnMissingBean(Helloservice.class)
    @Bean
    public Helloservice helloservice(){
        Helloservice helloservice = new Helloservice();
        return helloservice;
    }
    
}

Helloservice.java:

public class Helloservice {

    @Autowired
    private Helloproperties helloproperties;

    public String hello(String username){

        return helloproperties.getQian()+username+helloproperties.getHou();
    }

}

之后对spring boot项目clean和install,再对maven项目clean和install
在这里插入图片描述

步骤六:

这样starter就做好了
在测试项目中,在pom.xml配置文件中引入该starter:

<!--        自定义starter-->
        <dependency>
            <groupId>com.lin</groupId>
            <artifactId>lin-hello-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

这样starter自动配置也会被引入进来
测试:
在测试项目的application.properties配置文件中给starter中绑定属性的类赋值

lin.hello.qian=111
lin.hello.hou=222

控制器:

@RestController
public class HelloController {
    @Autowired
    private Helloservice helloservice;

    @GetMapping("/hello")
    public String hello() {
        String na = helloservice.hello("刘备");
        return na;
    }

}

运行成功
在这里插入图片描述

纯属个人经验,喜欢的可以点赞关注,后续见!!!

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到