侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 748 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

SpringBoot(28)之自定义starter

zze
zze
2018-07-13 / 0 评论 / 0 点赞 / 381 阅读 / 8621 字

在前一篇文章中我们了解了 SpringBoot 程序的启动流程后,我们也可以自己编写一个 starter 了,下面为一个小案例,功能是让程序在启动时注册一个 hello 组件,并且能通过配置修改输出内容。

创建自动配置组件

1、使用 Maven 创建一个 SpringBoot 程序作为自动配置组件,依赖如下:

<!-- 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.19.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zze.springboot</groupId>
    <artifactId>mystarter_autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mystarter_autoconfigure</name>
    <description>Demo project for Spring Boot</description>
    <packaging>jar</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

2、编写与配置文件属性映射的配置类:

// com.zze.springboot.config.HelloProperties
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "com.zze")
public class HelloProperties {
    private String name;
    private String remark;

    public String getName() {
        return name;
    }

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

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}

3、编写 hello 服务:

// com.zze.springboot.service.HelloService
package com.zze.springboot.service;

import com.zze.springboot.config.HelloProperties;

public class HelloService {

    private HelloProperties helloProperties;

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

    public void sayHello() {
        System.out.println("hello " + helloProperties.getName() + " " + helloProperties.getRemark());
    }
}

4、编写自动配置类:

// com.zze.springboot.autoconfigure.MyStarterAutoConfiguration
import com.zze.springboot.config.HelloProperties;
import com.zze.springboot.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 自定义自动配置类
 */
@EnableConfigurationProperties(HelloProperties.class) // 启用指定类的配置映射,并将配置类实例注册到 IoC 容器
@Configuration
@ConditionalOnWebApplication // web 应用时才生效
public class MyStarterAutoConfiguration {

    private HelloProperties helloProperties;

    public MyStarterAutoConfiguration(HelloProperties helloProperties){
        this.helloProperties = helloProperties;
    }

    // 注册 hello 服务到 IoC 容器
    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }

}

5、配置自动配置类,让程序启动时加载自动配置类让其生效:

# META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zze.springboot.autoconfigure.MyStarterAutoConfiguration

创建 starter 工程

1、创建普通的 Maven 项目作为 starter 组件,引入上面编写的自动配置组件依赖:

<!-- 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.zze.springboot</groupId>
    <artifactId>mystarter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--引入自动配置模块-->
        <dependency>
            <groupId>com.zze.springboot</groupId>
            <artifactId>mystarter_autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

2、将自动配置组件及 starter 组件安装到 Maven 仓库。

测试

1、使用 Maven 新创建一个 SpringBoot 项目,引入我们编写的 starter 依赖:

<!-- 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.19.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</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.zze.springboot</groupId>
            <artifactId>mystarter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

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

</project>

2、配置 hello 服务所需要的相关属性:

# application.properties
com.zze.name=张三
com.zze.remark=会员

3、直接注入 hello 服务,测试:

package com.example.demo;

import com.zze.springboot.service.HelloService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Autowired
    private HelloService helloService;

    @Test
    public void contextLoads() {
        helloService.sayHello();
        /*
        hello 张三 会员
         */
    }
}

小结

启动器模块是一个空 Jar 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或其它类库。启动器依赖于自动配置,第三方使用只需要引入启动器。

自动配置类能加载的前提是要将其配置在 META-INF/spring.factories 文件中的 org.springframework.boot.autoconfigure.EnableAutoConfiguration 节下。

0

评论区