当前位置:   article > 正文

【定制化体验:使用Spring Boot自动配置,打造个性化Starter】

【定制化体验:使用Spring Boot自动配置,打造个性化Starter】

项目结构

在这里插入图片描述

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

    <groupId>com.custom</groupId>
    <artifactId>spring-boot-starter-custom</artifactId>
    <version>0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <description>封装一个简单User组件</description>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.5.8</version>
        </dependency>
    </dependencies>
</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

spring.factories 注意书写格式

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.custom.config.UserAutoConfiguration
  • 1
  • 2
  • 3

UserService (最后注入成bean)

public class UserService {
    private String name;
    private String age;
    private Long id;


    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void pint() {
        System.out.println(String.format("获取的数据用户名:%s----密码:%s----id:%s", name, age, id));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

UserProperties (用户配置)

@ConfigurationProperties(prefix = "song.user")
public class UserProperties {
    private String start;
    private String name;
    private String age;
    private Long id;

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getStart() {
        return start;
    }

    public void setStart(String start) {
        this.start = start;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

最关键的配置 自动配置类 一定要看注释


/**
 * 功能描述: 自动配置类
 *
 * @author Songxianyang
 * @date 2024-04-26 13:40
 */
@Configuration
// yaml档中song.user.start 有没有  如果没有则无法自动配置改类
@ConditionalOnProperty(prefix = "song.user", name = {"start"})
// 把yml配置的属性注入进来 然后使用它。启动自动配置属性
@EnableConfigurationProperties(UserProperties.class)
public class UserAutoConfiguration {
    @Bean
    public UserService userService(UserProperties userProperties) {
        UserService userService = new UserService();
        userService.setId(userProperties.getId());
        userService.setName(userProperties.getName());
        userService.setAge(userProperties.getAge());
        return userService;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

如何使用自己封装的starter

随便找一个spring boot 项目 然后再pom中引入自己的starter

这里以我自己kkxx-poi 项目为例子
在这里插入图片描述

再写一个web控制层调用一下

在这里插入图片描述


关键配置yml (application-local.yml)

在这里插入图片描述

测试 读取到具体的配置拉

在这里插入图片描述

知识点汇总

  1. 自动配置原理
  2. Spring Boot环境选择
  3. yml配置的类
  4. 条件注解
  5. 开启关闭组件逻辑

代码分享

starter组件

调用组件的类

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号