- 首先创建一个springboot项目
- pom.xml导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
- 删除<build>标签所有内容
- 指定工程版本
-
-
包结构:
- DemoAutoConfiguration:自动配置类
- Demoproperties:配置文件
- DemoService:Demo测试对象
- META-INF/spring.factories(需要自己创建):springboot启动时会从这个文件找自动配置类
-
- 实现:
- DemoProperties
package org.mystarter.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 可在 .Properties 配置的属性
*/
@ConfigurationProperties(prefix = "spring.demo")//配置文件前缀
public class DemoProperties {
private String value ;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
- DemoService
package org.mystarter.service; import org.mystarter.properties.DemoProperties; public class DemoService { public DemoProperties demoProperties; /** * 返回 properties 的配置属性值 * @return */ public String getProperties(){ return demoProperties.getValue(); } }
- DemoAutoConfiguration
package org.mystarter.autoconfiguration;
import org.mystarter.properties.DemoProperties;
import org.mystarter.service.DemoService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({DemoProperties.class}) // 把DemoProperties配置文件加载进来
public class DemoAutoConfiguration {
/**
* 将demoService 放入IOC容器中
* @param demoProperties properties配置文件
* @return demoService对象
*/
@Bean
DemoService MyService(DemoProperties demoProperties){
DemoService demoService = new DemoService();
demoService.demoProperties = demoProperties;
return demoService;
}
}
- META-INF/spring.factories
#springboot启动时会扫 META-INF/spring.factoties 文件中 KEY为 org.springframework.boot.autoconfigure.EnableAutoConfiguration
# 的配置文件,实现自动配置 (Mybatis也是通过这个实现自动配置)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.mystarter.autoconfiguration.DemoAutoConfiguration
# Value 是自己的Configuration 路径
- 最后打包
- 通过命令打包
cxg@debian:~/IdeaProjects/demo-spring-boot-starter$ mvn clean package
- 通过自带打包操作
打包好的starter(jar)
放入你的maven仓库中就可以被其他项目使用
- 通过命令打包
-
使用(test)
-
创建一个springboot项目
-
pom.xml引入刚打包的starter
<dependency> <groupId>org.mystarter</groupId> <artifactId>demo-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency>
application.properties
# 应用名称 spring.application.name=mystartertest # 应用服务 WEB 访问端口 server.port=8080 #自定义starter的属性配置 spring.demo.value=myStarter
编写controller测试
package com.example.mystartertest.controller; import org.mystarter.service.DemoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 测试controller */ @RestController @RequestMapping("demos") public class DemoController { @Autowired DemoService service; /** * 使用自动注入的DemoService对象使用其getProperties方法 * @return 配置文件spring.demo.value的值 */ @GetMapping public String getDemoValue(){ return service.getProperties(); } }
-
到这里springboot自定义starter就结束了