springboot1.x升级到springboot3.x中遇到的问题总结

springboot1.x升级到springboot3.x中遇到的问题总结

前言

springboot是个很受欢迎的框架,减化了spring mvc的xml配置,现在springboot已经升级到了springboot 3.x,但还有一些老项目用的技术栈是java8+springboot 1.x。参照之前的项目配置去运行新版本的springboot 3会踩很多坑,技术栈的升级过程如果还按照原有的思维定式去操作是很难定位问题的。
下面总结了升级过程中遇到的一些问题。

问题:无法创建DataSource的bean对象,提示url或driver class未配置

问题详情:

APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

问题原因:引入了数据源相关的jar包,但是没有读到相关的配置。

解决方法:

  • 方法1:未使用数据库的项目可以,排除数据源相关的自动装配@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
  • 方法2:配置了运行环境参数,但没有创建对应的配置文件,需要创建对应的环境配置如:application-dev.yml。
  • 方法3:配置文件没有复制到输出目录(…\target\classes\application.yml),查看pom.xml中节点packaging的值是否为jar、war,如果值是pom将不会复制配置文件到输出目录。
  • 方法4:配置写在了bootstrap.yml文件中,Spring Boot 2.4版本开始,配置文件加载方式进行了重构,只会识别application.* 配置文件。

问题:引入freemark后页面总是报404

问题详情:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Dec 10 20:10:35 CST 2023
There was an unexpected error (type=Not Found, status=404).

解决方法:

  • 方法1:查看使用的模板后缀是否是.ftlh,springboot 2.2.0开始freemarker模板默认扩展名改为ftlh了。
  • 方法2:被@GetMapping注解修饰的方法,返回值是基于classpath的模板的路径,默认路径不用加后缀(如:“/test/index” 对应 “resources\templates\test\index.ftlh” 模板)。
  • 方法3:yml配置文件中配置模板路径,spring.freemarker.template-loader-path: classpath:/templates/

问题:bootstrap.yml不生效,配置中的内容无法读取

问题原因:
Spring Boot 2.4版本开始,配置文件加载方式进行了重构,只会识别application.* 配置文件,并不会自动识别bootstrap.yml;
因此如果springboot工程引入的是2.4以上的版本,则在resource下配置文件为application.yml
如果写入bootstrap.yml则不生效。

解决方案:

  • 方案一:spring-boot-starter-parent依赖降级,降到2.4以下版本;
  • 方案二:resource下写application.yml,不写bootstrap.yml;
  • 方案三:在pom.xml中引入如下依赖,并在启动类上添加注解@EnableDiscoveryClient;
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

配置文件类型:

  • application.properties
  • application.yml
  • application.yaml

配置文件优先级:

  • springboot 2.4.0及以后版本:
    • application.yaml > application.yml > application.properties
  • springboot 2.4.0以前版本
    • application.properties > application.yml > application.yaml

配置内容按优先级顺序读取,读到值后后续相同配置将忽略。旧版application.properties优先级最高,新版优先级最低。

配置文件不同位置优先级:

优先级(高到底)位置/路径描述说明
1file:config/application.ymljar包所在目录下config目录最高-运维经理
2file:application.ymljar包所在目录下config目录运维
3classpath:config/application.ymlresources目录下的config目录项目经理
4classpath:application.ymlresources目录最低-程序员
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值