欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

开发一个Spring Boot Starter!

程序员文章站 2023-11-15 19:30:34
在上一篇文章中,我们已经了解了一个starter实现自动配置的基本流程,在这一小结我们将复现上一过程,实现一个自定义的starter。 先来分析starter的需求: 在项目中添加自定义的starter依赖,自动在Spring中加载starter中的Bean; 从application.proper ......

在上一篇文章中,我们已经了解了一个starter实现自动配置的基本流程,在这一小结我们将复现上一过程,实现一个自定义的starter。

先来分析starter的需求:

  • 在项目中添加自定义的starter依赖,自动在spring中加载starter中的bean;
  • 从application.properties中加载指定配置

创建项目

  1. 先创建一个名为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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelversion>4.0.0</modelversion>

    <groupid>top.ninwoo</groupid>
    <artifactid>demo-starter</artifactid>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot</artifactid>
            <version>2.1.6.release</version>
        </dependency>
    </dependencies>
</project>
  1. 在resources中创建一个meta-inf的目录,并在目录中创建一个spring.factories。在这个配置中,我们只设置一个enableautoconfiguration项,并且对应只设置一个demoautoconfig配置类。
org.springframework.boot.autoconfigure.enableautoconfiguration=top.ninwoo.config.demoautoconfig  
  1. 创建demoautoconfig配置类

    package top.ninwoo.config;
    
    import org.springframework.boot.context.properties.enableconfigurationproperties;
    import org.springframework.context.annotation.bean;
    import org.springframework.context.annotation.configuration;
    
    @configuration
    @enableconfigurationproperties(demostarterproperties.class)
    public class demoautoconfig {
    
        @bean
        demobean demobean() {
            return new demobean();
        }
    }

    这个配置类,我们主要使用了@configuration和@enableconfigurationproperties两个注解。@enableconfigurationproperties启用一个configurationproperties。

  2. 创建configurationproperties对应的demostarterproperties

    package top.ninwoo.config;
    
    import org.springframework.boot.context.properties.configurationproperties;
    
    @configurationproperties(prefix = "top.ninwoo.demo")
    public class demostarterproperties {
    
        private string name = "default";
        private int age = 0;
    
        public string getname() {
            return name;
        }
    
        public void setname(string name) {
            this.name = name;
        }
    
        public int getage() {
            return age;
        }
    
        public void setage(int age) {
            this.age = age;
        }
    }

    创建一个configurationproperties类。这个类主要用来从application.properties中读取配置项,并自动设置到相对应的字段上。

  3. 创建一个测试用bean,并使用configurationproperties类中的信息。

    起初这里有个疑惑,不知道如何使用这个configurationproperties类。不过在spring中最常见的就是bean,我们可以大胆的猜测通过@configurationproperties注释的类,将自动在spring容器中自动创建一个bean。而我们在使用的时候,就通过普通的bean注入方式便可以使用configurationproperties类中的信息。所以,我们这样创建一个测试bean

    package top.ninwoo;
    
    import javax.annotation.resource;
    
    public class demobean {
        @resource
        demostarterproperties properties;
    
        public string getname() {
            return properties.getname();
        }
    
        public string getage() {
            return getage();
        }
    }
    

    同时在demoautoconfig中使用@bean注解创建一个bean。

到这里,我们的starter就创建完成了。通过mvn打包,或者创建同一个父项目的不同子module的方式,我们可以进行测试这个starter是否生效。

创建测试类

测试类使用一个spring boot web项目来完成,主要创建了一个restcontroller,并通过restcontroller获取spring上下文中注册的bean names和starter中的测试bean。

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>top.ninwoo</groupid>
    <artifactid>springboot-demo</artifactid>
    <version>1.0.0</version>

    <parent>
        <artifactid>spring-boot-starter-parent</artifactid>
        <groupid>org.springframework.boot</groupid>
        <version>2.1.6.release</version>
    </parent>

    <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
        </dependency>
        <dependency>
            <groupid>top.ninwoo</groupid>
            <artifactid>demo-starter</artifactid>
            <version>1.0-snapshot</version>
        </dependency>
    </dependencies>
</project>

在pom文件中,我们添加了刚刚实现的starter。

restcontroller:

@restcontroller
public class indexcontroller implements applicationcontextaware {

    applicationcontext ctx = null;

    @resource
    demobean demobean;

    @requestmapping("/getlist")
    public string[] getbeannames() {
        return ctx.getbeandefinitionnames();
    }

    @requestmapping("/getdemobean")
    public string demobean() {
        return demobean.getname();
    }
    @override
    public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception {
        ctx = applicationcontext;
    }
}

springboot启动类 mainapp:

package top.ninwoo;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;

@springbootapplication
public class mainapp {

    public static void main(string[] args) {
        springapplication.run(mainapp.class, args);
    }
}

我们可以看到,与正常的一个web项目相比,我们只是添加了一个依赖,而并没有修改启动类。

测试

访问127.0.0.1:8080/getlist接口,我们可以看到最后的几个bean names是:

...,"top.ninwoo.config.demoautoconfig","demobean","top.ninwoo.demo-top.ninwoo.config.demostarterproperties"]

这证明,通过注入我们starter依赖,已经在spring的上下文创建了starter配置类中的bean。

在没有设置application.properties时,直接访问http://127.0.0.1:8080/getdemobean,可以获取到测试用的bean实例中默认的参数配置default.

添加application.properties:

top.ninwoo.demo.name=joliu

重启项目,再次访问该接口,发现测试用的bean实例对应的属性已经安装配置类中的参数进行设置,返回了joliu。

小结

到这里,我们可以说已经了解了开发一个springboot starter最基本的流程,我们可以尝试在我们日常的项目中开发这样的starter。