创建一个spring项目

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

springboot自定义starter实践_配置文件

 按需添加必要的依赖

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


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


默认生成的项目结构如下

springboot自定义starter实践_配置文件_02

 

整理项目

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

删除默认生成的启动类

删除test文件

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

springboot自定义starter实践_spring boot_03

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

新建META-INF文件夹

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

新建spring.factories文件

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

最终项目结构如下

springboot自定义starter实践_java_04

注意检查! 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实践_java_05

 将项目打成一个jar包

springboot自定义starter实践_json_06

 引用

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


Mark Directory as Excluded


springboot自定义starter实践_配置文件_07

 然后再pom中引用

<dependency>
<groupId>cn.hello</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${pom.basedir}/src/main/resources/lib/hello-spring-boot-starter-0.0.1-SNAPSHOT.jar</systemPath>
</dependency>

具体业务类中引用

@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实践_配置文件_08

 

springboot自定义starter实践_spring_09