ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何编写自己的 Springboot starter

2022-03-20 15:58:21  阅读:222  来源: 互联网

标签:HelloProperties Springboot spring boot myapp 编写 public starter


编写自己的starter步骤

  1. 创建名字为xxx-spring-boot-starter 的启动器项目
  2. 创建名字为xxx-spring-boot-autoconfigure的项目
  • 编写属性绑定类xxxProperties
  • 编写服务类,引入xxxProperties
  • 编写自动配置类XXXAutoConfiguration注入配置
  • 创建spring.factories文件,用于指定要自动配置的类
  1. 启动器项目为空项目,用来引入xxx-spring-boot-autoconfigure等其他依赖
  2. 项目引入starter,配置需要配置的信息

创建启动器项目

由于启动器不需要代码实现,只需要依赖其他项目,所以直接创建一个空的 maven 项目。但是名字要规范。
这里创建的 startermyapp-spring-boot-starter
在这里插入图片描述myapp-spring-boot-starter

pom 文件非常简单,只需要引入接下来要创建的 myapp-spring-boot-autoconfigure

<?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>net.codingme.starter</groupId>
    <artifactId>myapp-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 启动器 -->
    <dependencies>
        <!--  引入自动配置项目 -->
        <dependency>
            <groupId>net.codingme.starter</groupId>
            <artifactId>myapp-spring-boot-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

创建自动配置项目

结合上面对 starter 的分析,直接创建一个名字为 myapp-spring-boot-autoconfigure 的项目。项目中只引入 springboot 父项目以及 spring-boot-starter

<?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.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>net.codingme.starter</groupId>
    <artifactId>myapp-spring-boot-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>myapp-spring-boot-autoconfigure</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
</project>

项目的总体结构看图
在这里插入图片描述myapp-spring-boot-starter-autoconfigure

HelloProperties中通过注解 @ConfigurationProperties(prefix = "myapp.hello")让类中的属性与 myapp.hello开头的配置进行绑定

/**
 * <p>
 *
 * @Author niujinpeng
 * @Date 2019/10/29 23:51
 */
@ConfigurationProperties(prefix = "myapp.hello")
public class HelloProperties {

    private String suffix;

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

然后在 HelloService中的 sayHello方法使用 HelloProperties 中自动绑定的值

public class HelloService {
    HelloProperties helloProperties;

    public String sayHello(String name) {
        return "Hello " + name + "," + helloProperties.getSuffix();
    }

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
}

为了让 HelloService 可以自动注入且能正常使用 HelloProperties,所以我们在
HelloServiceAutoConfiguration 类中把 HelloProperties.class 引入,然后把 HelloService 注入到 Bean

/**
 * web应用才生效
 */
@ConditionalOnWebApplication
/**
 * 让属性文件生效
 */
@EnableConfigurationProperties(HelloProperties.class)
/***
 * 声明是一个配置类
 */
@Configuration
public class HelloServiceAutoConfiguration {

    @Autowired
    private HelloProperties helloProperties;

    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

最后在 spring.factories中只需要指定要自动配置的类即可

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
net.codingme.starter.HelloServiceAutoConfiguration

到这里,自动配置项目就完成了

标签:HelloProperties,Springboot,spring,boot,myapp,编写,public,starter
来源: https://blog.csdn.net/qq_28807077/article/details/123614389

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有