赞
踩







- package com.example.demo.controller;
-
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- public class HelloController {
-
- @GetMapping("/helloworld")
- public String Hello(){
- return "Hello";
- }
- }


现在再回过头看看pom.xml文件,很明显可以看到,pom文件自带了四个部分parent ,本项目gav坐标,依赖,build
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.5.0</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
使用parent标签来引入父依赖的坐标,更好的管理项目jar包的版本,查看下parent项目里有什么?
parent项目中也引入了一个项目依赖,它就是版本仲裁中心。
spring-boot-dependencies,它里面管理了常用组件的版本号,子项目中使用maven坐标gav就无需添加版本号了
项目自己的gav坐标,使用maven将项目打成jar包时,就会用到这些信息。除了maven坐标的gav是必写的,其他的是可选项。比如name,项目的名字,description关于项目的描述。
- <groupId>com.example</groupId>
- <artifactId>demo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>demo</name>
- <description>Demo project for Spring Boot</description>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
spring-boot-starter-test
此包就是springboot集成的junit单元测试,在test包下就可以写方法来测试接口,不是本文的重点,直接无视。
spring-boot-starter-web
可以思考下,前面我们HelloController可以正常返回Hello,项目的运行是需要jar包的支持的,可是依赖是从哪里来的?
@RestController注解中包含@Controller和@ResponseBody
验证一下以上观点:在yaml文件中,修改端口号,再次运行启动类

说明springboot的依赖中有相关的自动配置的jar包
spring-boot-starter-web包下有五个直接依赖
关键词:
1.org.springframework.boot:spring-boot-starter:2.5.0

org.springframework.boot:spring-boot:2.5.0
它是一个空项目,里面没有java代码,只用一个pom文件引入其他依赖(spring,log4j,自动配置类相关依赖),具体就不深究了。
spring-webmvc

跟servlet有关的包
在pom.xml中引入spring-boot-starter-web依赖启动器时,就可以实现Web场景开发,而不需要额外导入Tomcat服务器以及其他Web依赖文件等。当然,这些引入的依赖文件的版本号还是由spring-boot-starter-parent父依赖进行的统一管理
但是mybatis依赖是没有的,需要手动添加
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。