快速开发一个自定义SpringBoot Starter

参照:http://www.justdojava.com/2019/07/20/java-springboot-starter/#

1.SpringBoot Starter

Starters are a set of convenient dependency descriptors that you can include in your application.

2.创建starter

2.1 创建springboot工程zsdy-json-spring-boot-starter,并加入自动装配spring-boot-autoconfigure和fastjson的依赖

<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.zsdy</groupId>
    <artifactId>zsdy-json-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>
    </dependencies>
</project>

2.2 创建MyJsonService

package com.zsdy.json;

import com.alibaba.fastjson.JSON;

public class MyJsonService {

    private String name;

    /**
    * 使用 fastjson 将对象转换为 json 字符串输出
    */
    public String objToJson(Object object) {
        return getName() + JSON.toJSONString(object);
    }

    public String getName() {
        return name;
    }

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

2.3 创建配置类MyJsonProperties

package com.zsdy.json;

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

@ConfigurationProperties(prefix = "ziyou.json")
public class MyJsonProperties {

    public static final String DEFAULT_NAME = "ziyou";

    private String name = DEFAULT_NAME;

    public String getName() {
        return name;
    }

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

2.4 创建自动化配置类

package com.zsdy.json;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass({MyJsonService.class})
@EnableConfigurationProperties(MyJsonProperties.class)
public class MyJsonAutoConfiguration {

    /**
    * 注入属性类
    */
    @Autowired
    private MyJsonProperties myJsonProperties;

    /**
    * 当当前上下文中没有 MyJsonService 类时创建类
    */
    @Bean
    @ConditionalOnMissingBean(MyJsonService.class)
    public MyJsonService myJsonService() {
        MyJsonService myJsonService = new MyJsonService();
        myJsonService.setName(myJsonProperties.getName());
        return myJsonService;
    }
}

2.5 创建spring.factories(resource/META-INF/),增加自动装配的类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zsdy.json.MyJsonAutoConfiguration

2.6 然后我们通过运行mvn install命令,将这个项目打包成 jar 部署到本地仓库中,提供让另一个服务调用,自此starter就构建成功了。

3.调用自创建的SpringBoot Starter(和其它SpringBoot Starter调用一样)

3.1 创建一个新的 SpringBoot web项目zsdy-json,并提供一个接口进行测试

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zsdy</groupId>
    <artifactId>zsdy-json</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>zsdy-json</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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.zsdy</groupId>
            <artifactId>zsdy-json-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
        </dependency>

    </dependencies>

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

</project>

3.2 配置application.properties

server.port=8080
ziyou.json.name=json-zsdy

3.3 测试接口

package com.zsdy.controller;

import com.zsdy.enity.User;
import com.zsdy.json.MyJsonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author zsdy
* @create 2019-08-02 11:39
**/
@RestController
public class JsonController {

    @Autowired
    private MyJsonService myJsonService;

    @RequestMapping(value = "json")
    public String beanToJson() {
        User user = new User();
        user.setName("小明");
        user.setAge(20);
        String toJson = myJsonService.objToJson(user);
        return toJson;
    }
}

3.4 启动zsdy-json项目,打开浏览器访问接口 http://localhost:8080/json 可以看到如下效果。

json-zsdy{"age":20,"name":"小明"}

至此,我们就完成了快速开发一个自定义SpringBoot Starter.