自定义SpringBootStarter
自定义SpringBootStarter
说明
启动器模块是一个 空 jar 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库;
-
命名归约:
-
官方命名:
-
前缀:spring-boot-starter-xxx
-
比如:spring-boot-starter-web....
-
-
自定义命名:
-
前缀:xxx-spring-boot-starter
-
比如:mybatis-spring-boot-starter
-
-
编写启动器
-
在IDEA中新建一个空项目 spring-boot-starter-diy
-
新建一个普通Maven模块:hello-spring-boot-starter
-
新建一个Springboot模块:hello-spring-boot-starter-autoconfigure
-
点击apply即可
-
在的 starter模块中导入autoconfigure模块的依赖
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>hello-spring-boot-starter-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
-
将 autoconfigure 模块下多余的文件(src/main和pom以外的文件)都删掉,pom中只留下一个 starter,这是所有的启动器基本配置
<?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.3.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>hello-spring-boot-starter-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hello-spring-boot-starter-autoconfigure</name> <description>hello-spring-boot-starter-autoconfigure</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> </project>
-
编写HelloProperties 配置类
@ConfigurationProperties(prefix = "wang.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; } }
-
编写一个自己的服务
public class HelloService { HelloProperties helloProperties; public HelloProperties getHelloProperties() { return helloProperties; } public void setHelloProperties(HelloProperties helloProperties) { this.helloProperties = helloProperties; } public String sayHello(String name) { return helloProperties.getPrefix() + name + helloProperties.getSuffix(); } }
-
编写我们的自动配置类并注入bean,测试!
@Configuration @ConditionalOnWebApplication //web应用生效 @EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration { @Autowired HelloProperties helloProperties; @Bean public HelloService helloService() { HelloService service = new HelloService(); service.setHelloProperties(helloProperties); return service; } }
-
在resources编写一个自己的
META-INF\spring.factories
(自己创建文件)# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.hellospringbootstarterautoconfigure.com.config.HelloServiceAutoConfiguration
-
编写完成后,可以安装到maven仓库中
maven install (idea可点击侧边 maven 的 install 按钮)
注意是hello-spring-boot-starter项目进行此操作
测试
新建项目测试我们自己的写的启动器
-
新建一个SpringBoot 项目
spring-boot-start-diy-test
-
导入我们自己写的启动器
<!-- 记得spring-boot-starter-web 依赖,因为我们加了@ConditionalOnWebApplication注解 --> <dependency> <groupId>org.example</groupId> <artifactId>hello-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
-
编写一个 HelloController 进行测试我们自己的写的接口!
@RestController public class HelloController { @Autowired HelloService helloService; @RequestMapping("/hello") public String hello(){ return helloService.sayHello("❤"); } }
-
编写配置文件 application.properties
wang.hello.prefix=WJ wang.hello.suffix=WY
-
启动项目进行测试,结果成功
GET http://localhost:8080/hello WJ❤WY
本文作者:Zzzy君不见
本文链接:https://www.cnblogs.com/Zzzyyw/p/16032674.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现