Spring应用程序看不到带有属性扩展名的文件(application.properties除外)

我正在开发一个Spring Boot Web应用程序,该应用程序需要从 homepage.properties 文件中获取一些数值数据。

尽管.properties文件位于同一文件夹内,但是thymeleaf模板视图不会呈现 application.properties 以外的文件的任何属性。

  

src> main>资源> application.properties(成功获取属性)

     

src> main>资源> homepage.properties(不获取属性)

这是一个简单的用法:

application.properties

working.hours=650

homepage.properties

test.hours=30

index.html

<h1 class="lan_fun_value mb-1" th:text="${@environment.getProperty('working.hours)}"></h1> //renders 650
<h1 class="lan_fun_value mb-1" th:text="${@environment.getProperty('test.hours)}"></h1> //renders nothing

任何想法可能是什么问题?

an_day 回答:Spring应用程序看不到带有属性扩展名的文件(application.properties除外)

您可以使用@PropertySources来加载多个属性文件。您可以在应用程序类上方编写以下代码。

@SpringBootApplication
@PropertySources({
        @PropertySource("application.properties"),@PropertySource("homepage.properties")
})
public class Application{

}

您必须在working.hourstest.hours之后添加单引号。

,

您可以直接通过@PropertySource("homepage.properties")

添加它

或通过@Value直接注入

@Value( "${test.hours=30}")
private String testHours;

或者如果您在Spring中使用xml:

<context:property-placeholder location="application.properties,homepage.properties"/>

或在命令运行应用程序时将其作为参数传递。

本文链接:https://www.f2er.com/3048089.html

大家都在问