Spring_boot_pom.xml和启动方式

本文深入探讨了Spring Boot如何通过spring-boot-starter-parent整合第三方框架,解释了spring-boot-starter-web的工作原理及其与Maven依赖的关系。文章还介绍了@RestController注解在微服务中的应用,以及Spring Boot的启动流程。

spring-boot-starter-parent  整合第三方常用框架信息(各种依赖信息)

 

spring-boot-starter-web   是Springboot整合SpringMvc Web  实现原理:Maven依赖继承关系

  相当于把第三方常用maven依赖信息,在parent项目中已经封装好了 提供依赖信息关联整合的jar包

  springboot中快速原理(Maven子父依赖关系),springboot对常用依赖信息进行封装

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
在pom中为什么不写版本号,因为在parent已经封装好 了 </dependency>

 

#springcloud 依赖于springboot实现微服务###springboot默认集成springmvc组件

springcloud使用springmvc 编写微服务http接口 微服务通讯接口(http+json格式)

 

@RestController注解  表示该类中的方法返回json格式相当于@Conytoller+@ResponseBody

由spring mvc 4.0后提供的注解,为微服务提供返回json格式

springboot启动原理,springmvc注解方式启动  内置http服务器(默认是tomcat服务器)

 

@EnableAutoConfiguration 

注解作用:扫包范围  默认当前类里面

public static void main(String[] args) {
//整个程序入口,启动springboot项目 创建内置tomcat服务器 使用tomcat加载springmvc注解启动类 SpringApplication.run(SpringBootDemoApplication.
class, args); }

 

@ComponentScan(basepackages={" "," "})

缺点:如果扫描的包比较多的,写起来很麻烦

 

@SpringBootAoolication

等于@EnableAutoConfiguration+@ComponentScan   同级包和当前包

 

转载于:https://www.cnblogs.com/tanlei-sxs/p/10242950.html

<think>好的,用户现在的问题是关于如何在pom.xml中添加spring-boot-starter的依赖。让我先仔细看一下用户提供的引用内容,以及之前的问题记录。 用户提到的引用[1]中提到,他们在使用Spring Boot 1.5.9版本时,添加了spring-boot-starter-data-jpa依赖,但无法使用@Entity。这可能是因为依赖配置不正确或者版本问题。同时,引用[2][3]解释了spring-boot-starter-parent的作用,说明父POM提供了依赖管理默认配置,这可能是正确配置依赖的关键。 用户现在的问题是如何在pom.xml中添加spring-boot-starter的依赖。首先,我需要确定用户具体需要哪个starter。Spring Boot有多个starter,比如web、data-jpa等。根据常见情况,可能用户需要的是基本的starter,但更常见的是web starter。不过用户之前的问题是关于data-jpa的,可能需要更具体的指导。 根据引用[3],添加依赖时不需要指定版本号,因为父POM已经管理了版本。所以正确的做法是在dependency中省略version标签,让父项目自动管理版本。例如,引用[3]中的示例展示了如何添加spring-boot-starter-web依赖,没有版本号。 另外,用户可能在父项目使用了旧版本1.5.9,这可能导致依赖冲突或缺失。引用[1]提到他们使用的parent是1.5.9,但可能现在需要升级到更新的版本,比如2.x,特别是如果用户遇到了@Entity的问题,这可能是因为JPA API的版本不兼容。不过用户当前的问题只是关于如何添加依赖,所以可能不需要涉及版本升级,但需要注意父版本是否正确。 另外,引用[5]提到了镜像配置的问题,可能需要检查Maven的settings.xml是否有正确的仓库配置,以避免依赖下载失败。但这个问题可能属于网络配置,用户当前的问题可能更多是依赖配置的语法问题。 总结步骤: 1. 确认父项目是否正确使用spring-boot-starter-parent,并指定了正确的版本。 2. 在dependencies部分添加所需的starter依赖,如spring-boot-starter-web,不需要指定版本。 3. 如果遇到@Entity问题,可能需要检查是否正确引入了spring-boot-starter-data-jpa,并且父版本是否支持JPA 2.x,因为旧版本可能依赖较旧的API。 可能用户之前的错误在于同时添加了persistence-api 1.0,这可能是过时的,应该依赖Spring Boot管理的JPA版本。所以在回答中,除了添加starter的步骤外,还需要提醒避免手动添加过时的依赖,而是依靠父POM的依赖管理。 另外,用户可能需要示例代码,所以在回答中提供具体的pom.xml片段,展示如何正确配置parentdependency,同时指出常见的错误,比如指定版本号或缺少必要的依赖。 最后,生成相关问题,帮助用户深入解决可能遇到的其他问题,比如版本冲突、依赖管理原理等。</think>[用户可见层] ### 如何在pom.xml中添加Spring Boot Starter依赖 #### 步骤说明 1. **确认父项目配置** 在`pom.xml`中必须声明`spring-boot-starter-parent`作为父项目,以继承Spring Boot的默认依赖管理插件配置[^2][^3]。 示例配置: ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.0</version> <!-- 推荐使用较新版本 --> <relativePath/> </parent> ``` 2. **添加Starter依赖** - 在`<dependencies>`标签内添加需要的Starter依赖,**无需手动指定版本号**(由父项目统一管理)[^3]。 - 例如添加Web模块依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 3. **避免常见错误** - 若手动指定版本号(如`<version>1.5.9</version>`),可能导致依赖冲突或API不兼容[^1]。 - 不要额外添加旧的JPA依赖(如`persistence-api 1.0`),应直接使用`spring-boot-starter-data-jpa`。 #### 完整配置示例 ```xml <?xml version="1.0" encoding="UTF-8"?> <project> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.0</version> </parent> <dependencies> <!-- Web模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- JPA模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> </project> ``` #### 注意事项 - 若出现依赖下载失败,需检查Maven镜像配置(例如阿里云镜像)[^5]。 - 旧版本(如1.5.9)可能导致JPA注解(如`@Entity`)无法识别,建议升级至Spring Boot 2.x。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值