我正在开发一个Spring Boot Web应用程序,它需要从homepage.properties文件中获取一些数字数据。
但是,thymeleaf 模板视图不会呈现除 application.properties以外的文件的任何属性,即使 .properties 文件位于同一文件夹中也是如此。
SRC> 主要>资源>应用程序属性(成功获取属性(
src> 主页>的主要>资源.properties(不获取属性(
这是一个简单的用法:
应用程序属性
working.hours=650
主页.属性
test.hours=30
索引.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
任何想法可能是什么问题?
您可以使用@PropertySources
加载多个属性文件。您可以在应用程序类上方编写下面的代码。
@SpringBootApplication
@PropertySources({
@PropertySource("application.properties"),
@PropertySource("homepage.properties")
})
public class Application{
}
您必须在working.hours
和test.hours
之后添加单引号。
您可以直接通过@PropertySource("homepage.properties")
添加它
或者通过@Value
来严重注射它
@Value( "${test.hours=30}")
private String testHours;
或者,如果您将 xml 与 spring 一起使用:
<context:property-placeholder location="application.properties, homepage.properties"/>
或者在命令运行应用时将其作为参数传递。