自制作spring boot starter

spring boot starter

代码

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

自制starter

maven项目依赖,只需依赖


    org.springframework.boot
    spring-boot-starter
    2.4.0
    compile

配置外部化

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


   com.chase
   common-spring-boot-starter
   1.0-SNAPSHOT

配置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已经完成

你可能感兴趣的:(自制作spring boot starter)