springboot-自动配置

本文探讨了Spring Boot的自动配置原理,通过查看源码解析了自动配置的工作流程,包括加载`spring.factories`文件、检查classpath中是否存在配置类以及如何使用`@EnableAutoConfigurationImportSelector`和`SpringFactoriesLoader`。同时,文章提到了自定义扩展Spring Boot starter的方法,重点关注了主要的config类以及`@Conditional`注解的使用,它是Spring4引入的条件注解,Spring Boot通过`SpringBootCondition`对其进行增强,实现按需注册bean。
摘要由CSDN通过智能技术生成

最近项目是不是很忙所以有时间看下springboot的自定配置,遂在这里做一下记录,待日后忘记时查阅。翻了下源码 查阅了一些资料了解了下关于spring boot autoconfig(后面简称 autoconfig)的相关内容。springboot框架本身有一套自己支持的autoconfig在 org.springframework.boot.autoconfigure包下META-INF/spring.factories文件中

自动配置的方式是:先加载spring.factories文件中的配置检测classpath中是否存在配置文件对应的类如果存在就加载不存在就跳过。
下面来看我查看源码的路径

@SpringBootApplication  >>>>> @EnableAutoConfiguration >>> AutoConfigurationImportSelector 

.EnableAutoConfigurationImportSelector类使用了Spring Core包的SpringFactoriesLoader类loadFactoryNamesof()方法.SpringFactoriesLoader会查询META-INF/spring.factories文件中包含的JAR文件当找到spring.factories文后SpringFactoriesLoader将查询配置文件命名的属性.然后通过调用 process()方法获取需要加载的类 。

public void process(AnnotationMetadata annotationMetadata,
			DeferredImportSelector deferredImportSelector) {
   
			Assert.state(
				deferredImportSelector instanceof AutoConfigurationImportSelector,
				() -> String.format("Only %s implementations are supported, got %s",
	        AutoConfigurationImportSelector.class.getSimpleName(),deferredImportSelector.getClass()
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用Mybatis-Plus自动生成代码的步骤如下: 1. 首先,在pom.xml文件中添加Mybatis-Plus的依赖项。这包括mybatis-plus-generator、velocity-engine-core和lombok等依赖项。\[1\] 2. 创建一个Mapper接口,继承自BaseMapper,并指定实体类的泛型。在这个接口中,你可以定义自己的查询方法。\[3\] 3. 配置Mybatis-Plus的代码生成器。你可以使用代码生成器来生成Mapper、Model、Service和Controller层的代码。你可以使用代码或者Maven插件来快速生成代码。\[2\] 4. 运行代码生成器,生成所需的代码文件。 5. 在Spring Boot的配置文件中配置数据库连接信息和Mybatis-Plus的相关配置。 6. 在Service层中使用生成的Mapper接口进行数据库操作。 通过以上步骤,你可以在Spring Boot中使用Mybatis-Plus自动生成代码。这样可以大大减少手动编写重复的CRUD操作的工作量,并提高开发效率。 #### 引用[.reference_title] - *1* [SpringBoot中的自动代码生成 - 基于Mybatis-Plus](https://blog.csdn.net/Jalon2015/article/details/116026730)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [springboot整合mybatis-plus,代码自动生成](https://blog.csdn.net/qq_32784303/article/details/82964168)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值