Spring 引导消息源自动配置
Spring Boot MessageSourceAutoConfiguration
我有问题或者我可能在 Spring Boot.我现在不确定。
我有一个具有以下依赖项的项目
- spring-boot-starter-thymeleaf
- spring-boot-starter-web
- spring-boot-starter-actuator
- spring-boot-starter-mail
- spring-cloud-starter-bus-amqp
- spring-cloud-starter-config
- spring-cloud-starter-eureka
我想在我的 Thymeleaf 模板中使用 messageSource,所以我在 application.yml 中设置了以下内容:
spring:
messages:
basename: de/mycompany/messages/message
并在上面的包中放了一个message.properties和一个message_de.properties。但是替换没有用。
所以我调试了 MessageSourceAutoConfiguration 并发现 @Conditional(ResourceBundleCondition.class) 确实有效。它找到了我的资源并返回了 true。
所以我让打印调试报告并发现,它说
MessageSourceAutoConfiguration
- Bundle found for spring.messages.basename: de/mycompany/messages/message (MessageSourceAutoConfiguration.ResourceBundleCondition)
- @ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: all) found the following [messageSource] (OnBeanCondition)
所以已经定义了另一个messageSource Bean,但我想知道它是从哪里来的。所以我进一步调查并发现了以下日志输出:
AnnotationConfigEmbeddedWebApplicationContext : Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@60df7989]
实际上它来自方法 initMessageSource 中的 class AbstractApplicationContext(大约第 622 行)。
在那里它会检查名称为 "messageSource" 的 bean,如果找不到,它会创建上面提到的 DelegatingMessageSource.
我错过了什么吗?我是否必须做一些事情来让 MessageSourceAutoConfiguration 在这个 AbstractApplicationContext 之前发生?或者这真的是一个错误?
对于我自己,我通过简单地自己创建消息源作为@Bean 来修复它,但是使用自动配置会更聪明:)
问候克里斯蒂安
事实证明这是一个错误,希望它能尽快解决,在此处跟踪:
https://github.com/spring-cloud/spring-cloud-commons/issues/29
我有问题或者我可能在 Spring Boot.我现在不确定。 我有一个具有以下依赖项的项目
- spring-boot-starter-thymeleaf
- spring-boot-starter-web
- spring-boot-starter-actuator
- spring-boot-starter-mail
- spring-cloud-starter-bus-amqp
- spring-cloud-starter-config
- spring-cloud-starter-eureka
我想在我的 Thymeleaf 模板中使用 messageSource,所以我在 application.yml 中设置了以下内容:
spring:
messages:
basename: de/mycompany/messages/message
并在上面的包中放了一个message.properties和一个message_de.properties。但是替换没有用。 所以我调试了 MessageSourceAutoConfiguration 并发现 @Conditional(ResourceBundleCondition.class) 确实有效。它找到了我的资源并返回了 true。 所以我让打印调试报告并发现,它说
MessageSourceAutoConfiguration
- Bundle found for spring.messages.basename: de/mycompany/messages/message (MessageSourceAutoConfiguration.ResourceBundleCondition)
- @ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: all) found the following [messageSource] (OnBeanCondition)
所以已经定义了另一个messageSource Bean,但我想知道它是从哪里来的。所以我进一步调查并发现了以下日志输出:
AnnotationConfigEmbeddedWebApplicationContext : Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@60df7989]
实际上它来自方法 initMessageSource 中的 class AbstractApplicationContext(大约第 622 行)。 在那里它会检查名称为 "messageSource" 的 bean,如果找不到,它会创建上面提到的 DelegatingMessageSource.
我错过了什么吗?我是否必须做一些事情来让 MessageSourceAutoConfiguration 在这个 AbstractApplicationContext 之前发生?或者这真的是一个错误?
对于我自己,我通过简单地自己创建消息源作为@Bean 来修复它,但是使用自动配置会更聪明:)
问候克里斯蒂安
事实证明这是一个错误,希望它能尽快解决,在此处跟踪:
https://github.com/spring-cloud/spring-cloud-commons/issues/29