手写一个springboot starter

springboot的starter的作用就是自动装配。将配置类自动装配好放入ioc容器里。作为一个组件,提供给springboot的程序使用。

今天手写一个starter。功能很简单,调用starter内对象的一个方法输出"hello! xxx"

先来了解以下命名规范

  • 自定义 starter,工程命名格式为{xxx}-spring-boot-starter

  • 官方starter,工程命名格式为spring-boot-starter-{xxx}

1.新建一个普通的maven工程,引入springboot依赖(2.5.6),项目名为hello-spring-boot-starter

pom.xml

复制代码
<dependencies>
<!--很关键-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<version>2.5.6</version>
</dependency>
<!--springBoot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.5.6</version>
</dependency>
<!-- Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
<!--打pom包的插件-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
复制代码

 2.编写业务类HelloStarter 

复制代码
@Data                   //生成getset
@AllArgsConstructor     //生成有参构造器
public class HelloStarter {
    //业务类依赖的一些配置信息,通过spring.properties配置
    private HelloProperties helloProperties;

    public String hello() {
        return "hello! " + helloProperties.name;
    }
}
复制代码

 3.编写配置信息类

@ConfigurationProperties(prefix = "com.shuai.hello")  //绑定到spring.properties文件中
@Data
public class HelloProperties {
    String name;
}

4. 编写自动配置类,将所有对象注入到容器中。

复制代码
@Configuration      //表明这是一个springBoot的配置类
public class AutoConfiguration {

    @Bean           //将对象放入IOC容器中,对象名就是方法名
    public HelloProperties helloProperties(){
        return new HelloProperties();
    }
    @Bean           //将对象放入IOC容器中,对象名就是方法名
    public HelloStarter helloStarter(@Autowired HelloProperties helloProperties){ //自动注入
        return new HelloStarter(helloProperties);
    }
}
复制代码

5.为了让springboot容器扫描到配置类,建一个resource目录,一个META-INF文件夹和spring.factories文件

 在spring.factories文件中写入

#等号后面是配置类的全路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.config.AutoConfiguration

6.使用install打包到本地仓库

注意:

所以一般我们使用install打包

 7.导入项目中进行测试
       <dependency>
            <groupId>com.shuai</groupId>
            <artifactId>hello-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
使用starter中的对象
复制代码
@RestController
public class HelloController {
    @Autowired
    HelloStarter helloStarter;
    @RequestMapping("/hello")
    public String hello(){

        return helloStarter.hello();
    }
}
复制代码
spring.properties中配置信息
com.shuai.hello.name="starter"

访问http://localhost:8080/hello 测试结果

 

非常棒!你现在已经会写一个简单的starter了,但是这远远不够,后续还要加上@EnableConfigurationProperties,@EnableConfigurationProperties等注解,让你的starter更加健壮

 

寄语:我们所要追求的,永远不是绝对的正确,而是比过去的自己更好。

posted @   小猴子_X  阅读(1097)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示