springboot自定义starter实践

创建一个spring项目

仿照spring的规范,artifact命名为xxx-spring-boot-starter

springboot自定义starter实践_第1张图片

 按需添加必要的依赖

这里只作为测试,就按最低的需求来只勾选如下三个

lombok、spring-boot-configuration-processor、spring-boot-autoconfigure

默认生成的项目结构如下

springboot自定义starter实践_第2张图片

 

整理项目

因为我们项目最终是给其他项目依赖使用的,所以可以删除一些不需要用到的文件

删除默认生成的启动类

删除test文件

删除pom中多于的配置,主要是关于测试和打包成可启动类的

springboot自定义starter实践_第3张图片

 我这配置文件application.properties也没用上,也删了

新建META-INF文件夹

在resource文件夹下新建META-INF文件夹

新建spring.factories文件

META-INF文件夹下新建spring.factories备用

最终项目结构如下

springboot自定义starter实践_第4张图片

注意检查! spring.factories文件的颜色,如果颜色不对,说明配置有问题,会影响后面的引用!!!

新建Properties

新建HelloProperties,用于定义配置信息,主要包括定义配置文件的前缀和key

package cn.hello;

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

@ConfigurationProperties(prefix = "spring.hello")//定义配置文件前缀
@Data
public class HelloProperties {

    private String username;//配置的key
    private String age;
    private String address;
}

新建Service

新建HelloService,主要用于处理具体的业务

package cn.hello;

import org.springframework.stereotype.Component;

@Component
public class HelloService {

    private HelloProperties log2esProperties;


    public HelloService(HelloProperties log2esProperties) {
        this.log2esProperties = log2esProperties;

    }

    public void sayHello() {

        System.out.println(log2esProperties);
    }
}

新建AutoConfiguration

关键!配置自动装配

package cn.hello;


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;

import javax.annotation.Resource;

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "spring.hello", value = "enabled", matchIfMissing = true)
public class HelloServiceAutoConfiguration {

    @Resource
    private HelloProperties log2esProperties;

    @ConditionalOnMissingBean(HelloService.class)
    @Bean
    public HelloService log2esService(){
        return new HelloService(log2esProperties);
    }
}

编辑spring.factories

以便HelloServiceAutoConfiguration能被spring识别并管理

org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.hello.HelloServiceAutoConfiguration

一切准备就绪,运行 mvn install

springboot自定义starter实践_第5张图片

 将项目打成一个jar包

springboot自定义starter实践_第6张图片

 引用

随便挑选一个springboot项目,在其resources下新建一个lib文件夹,记得右键lib,

Mark Directory as Excluded

springboot自定义starter实践_第7张图片

 然后再pom中引用

        
            cn.hello
            hello-spring-boot-starter
            0.0.1-SNAPSHOT
            system
            ${pom.basedir}/src/main/resources/lib/hello-spring-boot-starter-0.0.1-SNAPSHOT.jar
        

具体业务类中引用


    @Autowired
    private HelloService helloService;

    @GetMapping("/helloService")
    //@SysOpLogAnnotation(content = "helloService", operation = "helloService")
    public JsonResult esService() {
        helloService.sayHello();
        JsonResult jsonResult = new JsonResult(true);
        jsonResult.put("article",null);
        return jsonResult;
    }

yml配置安排一下

spring:
  hello:
    username: pd
    age: 20
    address: 卡塔尔多哈

启动,测试

springboot自定义starter实践_第8张图片

 

springboot自定义starter实践_第9张图片

 

你可能感兴趣的:(java框架,spring-boot,IntelliJ,IDEA,spring,boot,java,spring)