我有两个项目 - 一个基于"get started" example,第二个来自spring-boot-samples . 我使用Maven构建并从Eclipse运行 . "spring-boot-samples"项目加载application.properties并显示由Controller命名的Velocity模板 . "get started"没有 .
application.properties(src / main / resources / application.properties)和模板(src / main / resources / templates / **)的相同文件结构,在Eclipse中将src / main / resources设置为“Use as Source Folder” . 相同的工作空间,相同的JRE .
我比较了.classpath和pom.xml,但没有发现任何可疑之处 . 显然有区别,但我在哪里看?
PS:我可以通过@PropertySources加载application.properties,但是
-
不应该是必要的(见那里的评论)
"spring-boot-samples"项目中不需要 -
对Velocity模板没有帮助
谢谢!
2 回答
要在spring boot中从application.properties获取值,我们需要指定一些注释 .
application.properties必须位于src / main / resources路径中
类必须包含@RestController注释
@Value(“$ ”)私有字符串名称;
啊,一个重要的区别:
@RestController直接提供响应,这意味着:String不会解析模板的名称,而是传递给浏览器 . 原因应该是@ResponseBody:
使用@Controller代替解决Velocity问题 .
编辑:
要关闭此线程:我将继续使用@PropertySources来获取application.properties,但没有它就无法工作 . 仅在类路径中使用application.properties是不够的 .