自制作spring boot starter

spring boot starter

代码

代码github链接 common
代码github链接 chase

自制starter

maven项目依赖,只需依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.4.0</version>
    <scope>compile</scope>
</dependency>

配置外部化

package common;


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

/**
 * @author chase
 */
@ConfigurationProperties(prefix = "common.service")
public class CommonProperties {

    private String username;
    private String password;


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

服务类编写

package common;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author chase
 */
@Service
public class CommonService {

    @Autowired
    private CommonProperties commonProperties;
    
    /**
     * 外部调用方法
     * @return
     */
    public String sayHello() {
        return commonProperties.getUsername() ;
    }
}

配置类

package common;

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

/**
 * @author chase
 */
@Configuration
@ConditionalOnClass(CommonService.class)
@ConditionalOnProperty(prefix = "common.service",value = "enable",matchIfMissing = true)
@EnableConfigurationProperties(CommonProperties.class)
public class CommonServiceAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public CommonService greeter() {
        return new CommonService();
    }
}

创建自动装配配置文件

创建在resources/META-INF目录下创建spring.factories文件,文件内容如下

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
common.CommonServiceAutoConfiguration

把该maven项目 install到本地仓库,有私服的可以上传到私服,至此一个简单的spring boot starter已制作完成

在spring boot项目中使用自制作的starter

pom引入自制的starter

<dependency>
   <groupId>com.chase</groupId>
   <artifactId>common-spring-boot-starter</artifactId>
   <version>1.0-SNAPSHOT</version>
</dependency>

配置starter的配置文件

在application.properties文件中,配置

common.service.username=admin
common.service.password=admin23

注入starter中的服务,并调用

package com.league.chase.web;


import common.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CommonController {


    @Autowired
    private CommonService commonService;

    @GetMapping("/sayHello")
    public String sayHello() {
        return commonService.sayHello();
    }
}

测试

http://localhost:8080/sayHello 输出 admin

一个简单的starter已经完成

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容