实际上,我对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;
}如果我使用这种配置运行我的应用程序,那么一切都很好。属性文件中的消息被注入Thymeleaf模板(如预期的那样)。
但是如果我更改创建消息源的方法的名称,重新启动我的应用程序并请求相同的页面.然后找不到来自我的web.properties文件的消息。
@Bean
public MessageSource webMessageSource() {
[...]
}为什么消息资源的bean名称(=方法名)对我的应用程序有这样的影响?
为什么Thymeleaf模板引擎找不到消息源webMessageSource?
发布于 2014-06-01 16:00:39
在深入研究Thymeleaf源代码之后,我发现在类SpringTemplateEngine中,Javadoc清楚地声明:
它还将{@link SpringMessageResolver}配置为消息解析器,并实现{@link MessageSourceAware}接口,以便让Spring自动设置应用程序使用的{@链接MessageSource} (bean需要id "messageSource")。如果需要重写这个Spring标准设置,则可以使用{@link #setTemplateEngineMessageSource(MessageSource)}。
重要的是bean需要有id "messageSource"。
正如@ShinichiKai所指出的,Spring文档中的这部分提到,Spring中的MessageSource bean的名称必须是messageSource。
https://stackoverflow.com/questions/23980767
复制相似问题