4 configuration spring thymeleaf
我实际上对我的Thymeleaf配置的一部分感到困惑.
我有一个属性文件位于classpath:/messages/web.properties.在我的配置中,定义了以下内容.
@Bean
public MessageSource messageSource() {
final ResourceBundleMessageSource messageSource;
messageSource = new ResourceBundleMessageSource();
messageSource.setDefaultEncoding("UTF-8");
messageSource.setBasename("messages/web");
return messageSource;
}
Run Code Online (Sandbox Code Playgroud)
如果我使用此配置运行我的应用程序一切正常.来自属性文件的消息被注入Thymeleaf模板(如预期的那样).
但是,如果我更改创建消息源的方法的名称,请重新启动我的应用程序并请求相同的页面...然后web.properties找不到我文件中的消息.
@Bean
public MessageSource webMessageSource() {
[...]
}
Run Code Online (Sandbox Code Playgroud)
为什么消息资源的bean名称(=方法名称)对我的应用程序有影响?
为什么webMessageSourceThymeleaf模板引擎无法找到消息源?
在Thymeleaf源代码中进行了一些挖掘后,我发现在课堂SpringTemplateEngine上Javadoc明确指出:
它还将{@link SpringMessageResolver}配置为消息解析器,并实现{@link MessageSourceAware}接口,以便让Spring自动设置应用程序中使用的{@link MessageSource}(bean需要具有id"messageSource").如果需要覆盖此Spring标准设置,则可以使用{@link #setTemplateEngineMessageSource(MessageSource)}.
重要的部分是bean需要有id"messageSource"
正如@ShinichiKai所指出的,Spring文档的这一部分提到了Spring中MessageSource bean的名称必须是messageSource.
| 归档时间: |
|
| 查看次数: |
3312 次 |
| 最近记录: |