带你手写starter组件,搞懂SpringBoot自动配置原理

问题阐述

最近有不少朋友问,使用SpringBoot和使用XML配置到底有什么区别。其实如果我们只从编写代码的逻辑来看,使用SpringBoot同样是按照三层架构,从控制层、逻辑层再到持久层进行开发,这似乎和过去没什么区别。那SpringBoot的优势到底在哪里呢?今天通过带各位手写一个SpringBoot中的starter启动器,来跟大家分析一下这个问题。

自动配置

SpringBoot的最大优势其实就在于【自动配置】。大家是否还记得,在过去XML配置SSM程序的时代,我们为了把Mybatis整合到Spring的容器中去,做的第一件事就是先配置一个数据源的bean,一般配置代码如下:

<!--1 使用商业的数据源  把数据源配置成bean-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/cs2105?characterEncoding=utf8"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
        <property name="maxPoolSize" value="10"/>
</bean>

但当我们使用SpringBoot整合Mybatis之后,我们还配置过数据源的bean吗?很明显,我们再也没有配置过了!那么上面这个数据源的bean,到底是谁帮我们配置到容器里去了呢?答案就是SpringBoot的自动配置!SpringBoot的一个核心优势就是:带有大量的自动化配置,大大简化了项目开发的过程,这就是SpringBoot相比传统XML文件配置的优势所在!

自动配置的原理

有的小伙伴又问了,那SpringBoot又是怎么实现自动配置的呢?接下来壹哥将会仿照【druid-spring-boot-starter】手写一个简单starter工程【yg-spring-boot-starter】,通过这样的一个starter项目,就能让各位理解自动配置是怎么实现的了。本案例的实现有以下几个核心步骤。

1. 创建Maven项目

首先我们要创建一个普通的maven工程,工程名为【yg-spring-boot-starter】,当然你也可以自定义名称,这无所谓。具体创建过程略过,相信大家都会。

2. 添加核心依赖

接下来我们在创建好的yg-spring-boot-starter工程中,添加以下核心依赖包:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
 
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

3. 创建数据源配置类

接着我们在【yg-spring-boot-starter】工程中,仿照DruidDatasource类,编写一个ZKXDatasource类。这里只是为了说明问题,让大家感受如何进行自动配置,所以只保留了最基本的驱动程序、数据库地址等几个关键属性,其他的代码并没有进行实现。

@Data
public class ZKXDataSources {
    private String driver;
    private String url;
    private String password;
    private String userName;
}

4. 编写配置类

然后我们要在【yg-spring-boot-starter】中,实现ZKXDatasource配置类,把ZKXDatasource配置成bean,并使用 @ConfigurationProperties注解一次性读取指定前缀的自定义配置内容。

 
@Configuration
public class AutoConfig {
 
    @Bean
    @ConfigurationProperties(prefix = "zkx.datasource")
    public DataSources getDataSources(){
        return new ZKXDataSources();
    }
    
}

5. 配置spring.factories文件

接着在【yg-spring-boot-starter】工程的resources目录下,创建META-INF文件夹,并且创建壹哥spring.factories清单文件。
在这里插入图片描述
在spring.factoies文件中要添加如下配置:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.qf.zds.config. AutoConfig 

这个配置的意思是,当我们的【yg-spring-boot-starter】组件被其他工程依赖时,这个文件中罗列出来的bean将会被自动配置

6. 在别的项目中添加新建的starter组件

现在我们重新创建一个SpringBoot工程项目,在这个新项目中添加我们上面手写的starter依赖【yg-spring-boot-starter】。

<dependency>
    <groupId>com.qf</groupId>
    <artifactId>yg-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

7. 配置yml文件

在新建测试工程的application.yml文件中,我们可以加上DataSources的配置信息。

zkx:
  datasource:
    driver: haha002
    url: baidu.com
    userName: zoukx
    password: 123

8. 进行测试

最后为了验证效果,我们可以编写一个junit测试用例,来检测ZKXDataSources有没有被自动配置成bean。

@Autowired
private ZKXDataSources dataSources;
 
@Test
public void test01(){
    System.out.println(dataSources);
}

测试结果如下:我们可以发现ZKXDataSources已经被自动配置成了bean,控制台上已经输出了我们配置的信息了。
在这里插入图片描述

总结

至此,我们就实现了手写SpringBoot项目中的starter组件,进而实现了自动配置。其实从仿照druid的Datasource类中就可以发现,SpringBoot的核心机制就是可以自动配置,通过大量的自动配置,大量的bean被自动配置到容器中,这就大大简化了过去手动配置bean的过程,从而简化整个项目的开发过程,这也就是SpringBoot的最大优势所在!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一个简单的示例,展示了如何一个Spring Boot Starter: 首先,创建一个 Maven 项目,并添加以下依赖项: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.5.4</version> </dependency> </dependencies> ``` 接下来,创建一个自定义的自动配置类,用于配置你的 Starter: ```java @Configuration @EnableConfigurationProperties(MyStarterProperties.class) public class MyStarterAutoConfiguration { private final MyStarterProperties properties; public MyStarterAutoConfiguration(MyStarterProperties properties) { this.properties = properties; } // 在此处定义你的自动配置逻辑 @Bean public MyStarterService myStarterService() { return new MyStarterService(properties); } } ``` 然后,创建一个属性类,用于将外部配置绑定到属性上: ```java @ConfigurationProperties("my.starter") public class MyStarterProperties { private String message; // 提供 getter 和 setter } ``` 最后,创建一个自定义的服务类,该服务类将在你的 Starter 中使用: ```java public class MyStarterService { private final MyStarterProperties properties; public MyStarterService(MyStarterProperties properties) { this.properties = properties; } public void showMessage() { System.out.println(properties.getMessage()); } } ``` 现在,你的 Spring Boot Starter 已经准备就绪了!你可以将其打包并使用在其他 Spring Boot 项目中。在其他项目的 `pom.xml` 文件中,添加你的 Starter 依赖: ```xml <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>my-starter</artifactId> <version>1.0.0</version> </dependency> </dependencies> ``` 然后,在你的应用程序中使用 `MyStarterService`: ```java @SpringBootApplication public class MyApplication implements CommandLineRunner { private final MyStarterService myStarterService; public MyApplication(MyStarterService myStarterService) { this.myStarterService = myStarterService; } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { myStarterService.showMessage(); } } ``` 这样,你就成功地创建了一个简单的 Spring Boot Starter!当其他项目引入你的 Starter 并运行时,将会输出预定义的消息。 当然,这只是一个简单的示例,真实的 Starter 可能包含更多的配置和功能。你可以根据自己的需求进行扩展和定制。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值