SpringBoot原理分析之起步依赖

近年来SpringBoot火的可是一塌糊涂,得益于它零配置,只需要在maven pom.xml引入相应的依赖就能启动了。这一技术的出现,解救了面向大量xml编程而无法自拔的程序猿们-。-

  一、初次使用SpringBoot, 我们都知道要在pom.xml中引入:

        <!-- 所有的springboot工程都必须继承spring-boot-starter-parent -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.9.RELEASE</version>
	</parent>

然后在dependencies中引入web开发所用的依赖:

                <!--web开发的起步依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

经过以上步骤就可以代替原本SSM框架的大量的xml配置,就可以进行web开发了。这感觉,太爽了。但是在爽的同时,我们有思考过为什么只需要这么少的配置就可以了呢?依赖的版本是如何控制的呢?

二、刨根究底

1.ctrl键+鼠标左键点击artifactId

我们就会进来spingboot的父依赖:

在这个xml文件下,我们会看到spring-boot-starter-parent  也有父依赖:spring-boot-dependencies。

接着ctrl+鼠标左键:进入

我们会发现:properties中规定了spring-boot-starter-parent  2.1.9RELEASE 所要用到的其他依赖的版本,我们需要用到某个依赖,只要在pom.xml 的dependency中引入该以来的组名和坐标即可,不需要操心版本问题。

2.版本问题解决了,那么SpringBoot是如何做到只需要引入spring-boot-starter-web就可以进行web开发了呢?

同样的方法,我们点进去spring-boot-starter-web:看到了spring-boot-starter-web的父依赖:

发现同样 <properties>标签下规定了spring-boot-starter-web  可能要用到的其他依赖的版本:

而且在<dependency>标签下,我们看到了spring-boot-starter-web 依赖了web开发所需要的其他jar包,

如:tomcat

如:freemarker

我们还看到了跟spring-boot-starter-web同名的一个依赖,如下图:

同样的方法点进去:我们看到了久违的web和webmvc

由此我们可以看出,我们只需要引入spring-boot-starter-web,springBoot框架就帮我们引入了web和webmvc.

综上:SpringBoot框架确实帮助我们干了很多活我们只需要引入

    <!-- 所有的springboot工程都必须继承spring-boot-starter-parent -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.9.RELEASE</version>
	</parent>
            <!--web开发的起步依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

以上两个依赖,就可以进行web开发啦!知其然,知其所以然!

发布了22 篇原创文章 · 获赞 21 · 访问量 2084

猜你喜欢

转载自blog.csdn.net/weixin_41532316/article/details/102489651