一、创建项目工程
在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文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.atguigu.starter</groupId> <artifactId>atguigu-spring-boot-starter-autoconfigurer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>atguigu-spring-boot-starter-autoconfigurer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--spring-boot-starter是每个starter需要引入的依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> </project>1、创建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文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.atguigu.starter</groupId> <artifactId>atguigu-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <!--启动器--> <dependencies> <!--引入自动配置模块--> <dependency> <groupId>com.atguigu.starter</groupId> <artifactId>atguigu-spring-boot-starter-autoconfigurer</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
三、测试
新建一个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
本文档详细介绍了如何在IntelliJ IDEA中创建并实现一个自定义的Spring Boot Starter。从创建项目、编写核心组件到配置自动配置类,再到测试验证,整个过程清晰明了。通过这个例子,读者可以学习到如何扩展Spring Boot的功能并将其整合到自己的项目中。






点击ok即可,项目创建完毕

6560

被折叠的 条评论
为什么被折叠?



