問題描述
是否可以通過在屬性文件中定義 @RequestMapping
注釋在 Spring 中定義它的值?
Is it possible to define the value of a @RequestMapping
annotation in Spring by defining it in a properties file?
實際上,我會這樣做:
@Controller
@RequestMapping("/xxx")
public class MyController {
...
}
但我想將路徑 /xxx
存儲在屬性文件中.為什么?例如,如果我在控制器中重命名路徑,我就不太可能在模板中做 mystakes.
But I would like to store the path /xxx
in a properties file. Why? For instance, it is less likely that I do mystakes in my templates if I rename the path in the controller.
在其他框架中這是允許的(例如,參見 Symfony).
In other framework this is allowed (see Symfony, for instance).
推薦答案
應該可以在 @RequestMapping
中使用占位符,例如 @RequestMapping("${foo.bar}")
.看看 文檔了解更多詳情:
It should be possible to use placeholders in @RequestMapping
, like for example @RequestMapping("${foo.bar}")
. Take a look at the documentation for more details:
@RequestMapping
注釋中的模式支持針對本地屬性和/或系統屬性和環境變量的 ${...
} 占位符.這在控制器映射到的路徑可能需要通過配置進行自定義的情況下可能很有用.有關占位符的更多信息,請參閱 PropertyPlaceholderConfigurer 類的 javadocs.
Patterns in
@RequestMapping
annotations support${…?
} placeholders against local properties and/or system properties and environment variables. This may be useful in cases where the path a controller is mapped to may need to be customized through configuration. For more information on placeholders, see the javadocs of the PropertyPlaceholderConfigurer class.
這篇關于Spring:在屬性文件中定義 @RequestMapping 值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!