手写一个stater_精通SpringBoot——第五篇:写一个spring-boot-starter包

本文详细介绍了如何从零开始创建一个名为spring-boot-hello-starter的自定义starter包,包括创建Properties类、服务类、AutoConfiguration类,配置spring.factories,打包并引入到本地仓库,以及在新的SpringBoot工程中进行测试。通过这个过程,读者可以深入理解SpringBoot的自动配置原理。
摘要由CSDN通过智能技术生成

为了能更好的理解Springboot的自动配置和工作原理,我们今天来手写一个spring-boot-hello-starter。这个过程很简单,代码不多。接下来我们看看怎么开始实践。

1. 新建maven工程。

这块就不演示了,如果不会可以自行百度...啦啦啦,因为太简单了啊

2.新建一个properties类

/**

* @author Lee

* @// TODO 2018/7/25-9:21

* @description

*/

@ConfigurationProperties(prefix = "customer")

public class CustomerProperties {

private static final String DEFAULT_NAME = "Lensen";

private String name = DEFAULT_NAME;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

2.创建一个服务类CustomerService

/**

* @author Lee

* @// TODO 2018/7/25-10:30

* @description

*/

public class CustomerService {

private String name;

public String findCustomer(){

return "The Customer is " + name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

3.AutoConfiguration类

/**

* @author Lee

* @// TODO 2018/7/25-10:32

* @description

*/

@Configuration

@EnableConfigurationProperties(CustomerProperties.class)

@ConditionalOnClass(CustomerService.class)

@ConditionalOnProperty(prefix = "customer", value = "enabled", matchIfMissing = true)

public class CustomerAutoConfiguration {

@Autowired

private CustomerProperties customerProperties;

@Bean

@ConditionalOnMissingBean(CustomerService.class)

public CustomerService customerService() {

CustomerService customerService = new CustomerService();

customerService.setName(customerProperties.getName());

return customerService;

}

}

4. spring.factories配置

在src/main/resources新建文件夹META-INF,然后新建一个spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

com.developlee.configurer.CustomerAutoConfiguration

pom文件修改artifactId为spring-boot-hello-starter 打包成jar. 然后用mvn install 安装到本地mvn仓库。

5. 测试spring-boot-hello-start

新建springboot工程,在pom.xml文件引入安装的jar包

com.developlee

spring-boot-hello-starter

1.0-SNAPSHOT

在项目启动类中直接做测试:

@SpringBootApplication

@RestController

public class TestStarterApplication {

@Autowired

CustomerService customerService;

@GetMapping("/")

public String index(){

return customerService.getName();

}

public static void main(String[] args) {

SpringApplication.run(TestStarterApplication.class, args);

}

}

application.properties文件配置下我们的customer.name

customer.name = BigBBrother

接下来启动项目,请求地址localhost:8080,即可看到页面上显示BigBBrother。

到这我们已经完成了一个spring-boot-starter jar包的开发,有很多功能我们可以自己封装成一个jar,以后要用直接引用就行了~ 这样写代码是不是会有不一样的体验呢?

最后,以上示例代码可在我的github.com中找到。

我的个人公众号:developlee的潇洒人生。

关注了也不一定更新,更新就不得了了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot Starter Parent 3.3 是 Spring Boot 中的一个核心组件,它是一个 Maven 或 Gradle 插件项目模板,用于简化创建新的 Spring Boot 应用程序的过程。Starter Parent 模板集成了各种依赖项(Starter),使得开发者可以快速启动应用,并通过添加额外的模块依赖来自定义应用程序的功能。 ### 主要特性 #### 统一配置 - **自动配置**:Starter Parent 提供了预先配置好的功能,默认含了常见的 Web、数据访问、邮件发送等配置。 - **共享依赖**:所有基于该父项目的子项目都会共享一套基础依赖,这有助于维护一致性和减少重复工作。 #### 自动化任务支持 - **构建自动化**:通常含了一些自动化构建的任务,比如打、测试、运行本地服务器等。 - **添加额外依赖**:用户可以根据需要,从大量可用的 Stater 中选择并添加到自己的项目中,以满足特定需求。 - **自定义配置**:允许用户覆盖或扩展默认配置,保持灵活性。 ### 使用场景 - **新项目启动**:适合于从零开始构建新的 Spring Boot 应用,可以快速搭建起基本框架和基础功能。 - **已有项目升级**:对于已经使用的较老版本 Spring Boot 的项目,迁移至更高版本时,通过引入相应的 Starter Parent 可以简化升级过程,保证兼容性同时更新至最新最佳实践。 - **团队协作**:在多开发者协同的环境中,统一的基础架构可以促进效率,减少不必要的配置差异。 ### 示例 假设你要创建一个新的 Spring Boot 应用,并使用 Spring Boot Starter Parent 3.3: ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.x.yourVersion</version> </parent> <!-- 引入具体的starter --> <dependency> <groupId>com.example</groupId> <artifactId>example-app</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- 添加你需要的其他依赖,如web、dataJpa、actuator等 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </dependency> ``` 这里的 `<yourVersion>` 需替换为你实际想要使用的版本号。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值