如何将 Spring Boot 项目迁移到遗留的 Spring MVC 项目面临问题,如何读取遗留 Spring MVC 项目中的 application.properties 文件

问题描述

我已经在 Spring Boot 应用程序中完成了 POC。但是我们的项目运行在传统的 Spring MVC 上。

我在将 Spring Boot 项目转换为 Spring MVC 以读取 application.properties 文件时遇到的问题。

Spring Boot application.properties 文件内容如下

okta.oauth2.issuer=https://dev-3038103.okta.com/oauth2/bus3zhc6ayn7hfzQN5d6
resourceServer.url=http://localhost:8082
okta.oauth2.clientId={client-id}
okta.oauth2.clientSecret={secret-id}
okta.oauth2.scopes=openid,profile
server.port=8080

谁能告诉我怎么处理?

解决方法

有很多方法可以实现这一点,

  • 使用 PropertyPlaceholderConfigurer
  • 使用 PropertySource
  • 使用 ResourceBundleMessageSource
  • 使用 PropertiesFactoryBean

这就像去源码一样。

注册 PropertyPlaceholderConfigurer bean-

<context:property-placeholder location="classpath:path/filename.properties"/>

我提供了第一种方法的实现,但是你可以很容易地了解其余的,遵循任何一个