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优先级最高,新版优先级最低。

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

优先级(高到底) 位置/路径 描述 说明
1 file:config/application.yml jar包所在目录下config目录 最高-运维经理
2 file:application.yml jar包所在目录下config目录 运维
3 classpath:config/application.yml resources目录下的config目录 项目经理
4 classpath:application.yml resources目录 最低-程序员

相关推荐

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

    2023-12-14 08:50:02       55 阅读
  2. SpringBoot2升级SpringBoot3总结

    2023-12-14 08:50:02       39 阅读
  3. jdk8与jdk17区别。springboot2.xspringboot3.x区别

    2023-12-14 08:50:02       45 阅读
  4. SpringBoot3.x项目忽略SSL证书错误

    2023-12-14 08:50:02       23 阅读
  5. 升级openssl3.X版本

    2023-12-14 08:50:02       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-14 08:50:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-14 08:50:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-14 08:50:02       82 阅读
  4. Python语言-面向对象

    2023-12-14 08:50:02       91 阅读

热门阅读

  1. 音频筑基:信噪比SNR指标

    2023-12-14 08:50:02       63 阅读
  2. arrays.sort用法详解

    2023-12-14 08:50:02       53 阅读
  3. Crow:黑魔法 添加路由3 绑定lambda

    2023-12-14 08:50:02       62 阅读
  4. 单词统计(C语言)

    2023-12-14 08:50:02       56 阅读
  5. Ray RLlib User Guides:模型,处理器和动作分布

    2023-12-14 08:50:02       60 阅读
  6. BIND9配置及配置文件参数详解

    2023-12-14 08:50:02       53 阅读
  7. vue axios 使用

    2023-12-14 08:50:02       58 阅读
  8. vue项目 实现 vue-schart 图表 resize

    2023-12-14 08:50:02       60 阅读
  9. 精通Nginx(23)-Nginx Plus增强功能之负载均衡

    2023-12-14 08:50:02       51 阅读
  10. shell编程系列(14)-正则表达式详解

    2023-12-14 08:50:02       54 阅读
  11. react Api之createContext

    2023-12-14 08:50:02       53 阅读