Springboot框架实现自动装配详解
作者:软件开发随心记 时间:2023-03-03 21:11:14
序言
springboot框架价值,可以简单快速的构建独立的spring生产级别应用。springboot主要有以下的特性:
1.创建独立的Spring应用
2.直接嵌入Tomcat等Web容器(不需要部署WAR文件)
3.提供固化的“starter”依赖,简化构建配置
4.当条码满足时自动装配Spring货第三方类库
5.提供运维的特性,如指标信息,健康检查和外部配置
6.不需要XML配置。
下面就以springboot启动的时候,是如何实现mybatis自动装配。
从程序的使用去入手分析
引入mybatis的starter依赖和相关的数据库驱动
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
程序使用
//程序的启动类
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
//服务类
@Service
public interface UserService {
@Autowired
private UserXmlMapper userXmlMapper;
@Test
public void testFindAll2() {
List<User> list = userXmlMapper.findAll();
System.out.println(list);
}
}
//Dao 操作
@Mapper
public interface UserXmlMapper {
public List<User> findAll();
}
上面的代码,大家都是很熟悉的,springboot是如何实现 @Mapper的注解,就可以操作数据库呢(就是中间有个桥梁,如何将springboot和mybatis连接起来的,而这个桥梁是如何实现的呢)
从启动类寻找桥梁
通过源码可以知晓
桥梁 =@SpringBootApplication=>@EnableAutoConfiguration=>@Import({AutoConfigurationImportSelector.class})
分析源码可知AutoConfigurationImportSelector的主要作用
扫描classpath下所有的jar包
在META-INF/spring.factories配置好的AutoConfiguration
扫描出来需要执行的AutoConfguration
回想再使用mybatis的时候,是需要引入mybatis的starter依赖包的,再结合AutoConfigurationImportSelector的第二点功能,可以到对应的jar下找到mybatis的AutoConfguration。
也就是说spring-boot启动【会创建个spring容器】的时候,会去执行MybatisAutoConfiguration的逻辑【处理@Mapper的类(扫描出来注入到srping容器)和数据库连接等功能】。
关于MybatisAutoConfiguration的具体功能的逻辑,这块是设计到spring源码的了。有兴趣的可以自行查找。
来源:https://blog.csdn.net/vipshop_fin_dev/article/details/126441002
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android Retrofit的使用详解
C#实现下载网页HTML源码的方法
详解SpringBoot+Lucene案例介绍
C#中的协变与逆变小结
![](https://img.aspxhome.com/file/2023/7/122667_0s.png)
Android 进程间通信实现原理分析
![](https://img.aspxhome.com/file/2023/3/122223_0s.gif)
Android中Intent组件的入门学习心得
![](https://img.aspxhome.com/file/2023/1/139241_0s.png)
MyBatis多对多关联映射创建示例
![](https://img.aspxhome.com/file/2023/0/128910_0s.jpg)
C#使用Unity实现剪刀石头布游戏
![](https://img.aspxhome.com/file/2023/7/89857_0s.jpg)
android surfaceView实现播放视频功能
Java中使用fileupload组件实现文件上传功能的实例代码
![](https://img.aspxhome.com/file/2023/8/99708_0s.png)
关于java中构造函数的一些知识详解
C# ref and out的使用小结
![](https://img.aspxhome.com/file/2023/1/106391_0s.png)
简介Winform中创建用户控件
Docker环境下Spring Boot应用内存飙升分析与解决场景分析
![](https://img.aspxhome.com/file/2023/5/119795_0s.jpg)
Spring Boot基础学习之Mybatis操作中使用Redis做缓存详解
![](https://img.aspxhome.com/file/2023/1/67961_0s.jpg)
Android UI设计之AlertDialog弹窗控件
![](https://img.aspxhome.com/file/2023/8/137618_0s.gif)
Java的System.getProperty()方法获取大全
spring aop execution表达式的用法
![](https://img.aspxhome.com/file/2023/2/88072_0s.png)
一篇文章带你入门Java数据类型
![](https://img.aspxhome.com/file/2023/8/62338_0s.jpg)