Spring-Boot微服务架构和实践spring boot

Spring Boot自定义Starter模块开发

2019-01-06  本文已影响2人  centychen

概述

众所周知,Spring Boot的核心理念是“约定高于配置”,这一理念最终落地就是通过Starter模块来实现的。个人理解各种Starter模块最主要作用包括3部分:依赖管理、约定配置和自动装配。

除了使用官方提供的Starter外,也可以实现自定义的Starter,比如说将一些公共模块通过封装成Spring Boot的Starter,其它项目使用是就会更加方便。

自定义Starter

创建项目

创建一个Gradle(本文使用gradle进行项目管理)项目custom-spring-boot-starter,引入以下依赖:

dependencies {
    compile (
            "org.springframework.boot:spring-boot-autoconfigure:2.0.5.RELEASE",
            "org.springframework.boot:spring-boot-configuration-processor:2.0.5.RELEASE"
    )
}

按照Spring官方的建议,Starter的命名规则如下:

==既然使用类Spring Boot,就要遵循“约定高于配置”的思想,Starter模块的命名建议还是按照约定来吧 ^_^!==

配置类

创建一个类 CustomProp.class,这是一个典型JavaBean。然后通过 @ConfigurationProperties 注解指定这是一个配置类,并设置注解的 prefix 属性值指定配置项的前缀。

package org.cent.starter.custom.prop;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

/**
 * @author: cent
 * @email: chenzhao@viomi.com.cn
 * @date: 2019/1/4.
 * @description: 自定义Starter配置类
 */
@ConfigurationProperties(prefix = "org.cent.stater.custom")
@Setter
@Getter
public class CustomProp {

    private String name = "cent";

    @NestedConfigurationProperty
    private String description = "天气真好";
}

业务Bean

package org.cent.starter.custom.service;

/**
 * @author: cent
 * @email: chenzhao@viomi.com.cn
 * @date: 2019/1/4.
 * @description: 示例业务接口
 */
public interface CustomService {

    /**
     * 示例方法
     */
    void sayHello();
}

package org.cent.starter.custom.service.impl;

import lombok.AllArgsConstructor;
import org.cent.starter.custom.prop.CustomProp;
import org.cent.starter.custom.service.CustomService;

/**
 * @author: cent
 * @email: chenzhao@viomi.com.cn
 * @date: 2019/1/4.
 * @description: 示例业务接口实现类
 */
@AllArgsConstructor
public class CustomServiceImpl implements CustomService {

    private CustomProp customProp;

    @Override
    public void sayHello() {
        String message = String.format("你好!%s,%s!", customProp.getName(), customProp.getDescription());
        System.out.println(message);
    }
}

自动化配置

package org.cent.starter.custom.autoconfig;

import org.cent.starter.custom.prop.CustomProp;
import org.cent.starter.custom.service.CustomService;
import org.cent.starter.custom.service.impl.CustomServiceImpl;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: cent
 * @email: chenzhao@viomi.com.cn
 * @date: 2019/1/4.
 * @description: 自动配置类
 */
@Configuration
@EnableConfigurationProperties(CustomProp.class)
public class CustomAutoConfiguration {

    /**
     * 初始化自定义Starter的Bean
     *
     * @param customProp
     * @return
     */
    @Bean
    public CustomService customService(CustomProp customProp) {
        return new CustomServiceImpl(customProp);
    }
}


# 自动初始化配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  org.cent.starter.custom.autoconfig.CustomAutoConfiguration

大功告成

至此,一个典型的自定义Starter就开发完成了,项目结构如下:


image.png

使用示例

创建示例项目

dependencies {
    compile "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
    compile "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
    compile project (":custom-spring-boot-starter")
}

项目启动类

给项目一个启动类,Spring Boot项目的启动类,这里就不说明了。

package org.cent.custom.starter.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author: cent
 * @email: chenzhao@viomi.com.cn
 * @date: 2019/1/4.
 * @description: 示例启动类
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

测试

package org.cent.example.test;

import org.cent.custom.starter.example.Application;
import org.cent.starter.custom.service.CustomService;
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;

/**
 * @author: cent
 * @email: chenzhao@viomi.com.cn
 * @date: 2019/1/4.
 * @description:
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class CustomServiceTest {

    @Autowired
    private CustomService customService;

    @Test
    public void sayHello(){
        customService.sayHello();
    }
}

在test的resources目录下,增加一个application.yml配置文件,文件配置如下:

org:
  cent:
    stater:
      custom:
        name: jack
        description: 这是使用自定义配置的

示例代码

码云:https://gitee.com/centy/demo-spring-boot-starter.git

参考文章

上一篇下一篇

猜你喜欢

热点阅读