一、创建项目工程
在IDEA中创建一个空项目,两个moudle,分别是sunshine-spring-boot-starter和sunshine-spring-boot-starter-autoconfigurer
1、创建一个空项目
点击finish
创建moudle
新建一个sunshine-spring-boot-starter maven项目
点击finish即可
再新建一个moudle为sunshine-spring-boot-starter-autoconfigurer
点击finish即可
点击ok即可,项目创建完毕
二、实现自定义starter
(1)首先将sunshine-spring-boot-starter-autoconfigurer中启动类删除,以及test文件夹。这些可以不需要
pom文件
4.0.0 org.springframework.boot spring-boot-starter-parent2.5.5 com.atguigu.starter atguigu-spring-boot-starter-autoconfigurer0.0.1-SNAPSHOT atguigu-spring-boot-starter-autoconfigurer Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter1、创建HelloService
package com.atguigu.starter; public class HelloService { HelloProperties helloProperties; public String say(String name) { return helloProperties.getPrefix() + "-" + name + helloProperties.getSuffix(); } public HelloProperties getHelloProperties() { return helloProperties; } public void setHelloProperties(HelloProperties helloProperties) { this.helloProperties = helloProperties; } }2、创建HelloProperties
package com.atguigu.starter; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "atguigu.hello") public class HelloProperties { private String prefix; private String suffix; public String getPrefix() { return prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } }3、创建HelloServiceAutoConfiguration
package com.atguigu.starter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnWebApplication // web应用才生效 @EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration { @Autowired HelloProperties helloProperties; @Bean public HelloService helloService() { HelloService helloService = new HelloService(); helloService.setHelloProperties(helloProperties); return helloService; } }4、创建meta_INF文件夹并创建spring.factories文件
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.atguigu.starter.HelloServiceAutoConfiguration(2)在sunshine-spring-boot-starter的pom文件中引入atguigu-spring-boot-starter-autoconfigurer
pom文件
4.0.0 com.atguigu.starter atguigu-spring-boot-starter1.0-SNAPSHOT com.atguigu.starter atguigu-spring-boot-starter-autoconfigurer0.0.1-SNAPSHOT
三、测试
新建一个web项目
在application.properties添加内容
atguigu.hello.prefix=sunshine atguigu.hello.suffix=Hello World新建HelloController类
package com.atguigu.controller; import com.atguigu.starter.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired private HelloService helloService; @GetMapping("/hello") public String hello() { return helloService.say("你好"); } }测试项目可以正常启动
在浏览器中输入http://localhost:8080/hello
结果为 sunshine-你好Hello World



