Java Spring 启动应用程序和 MessageSource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46659679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot application and MessageSource
提问by nicolas
I have been trying to create a demo spring boot app with MessageSource, but i couldn't figure out what is the problem. I tried in couple of ways:
我一直在尝试使用 MessageSource 创建一个演示 Spring Boot 应用程序,但我无法弄清楚是什么问题。我尝试了几种方法:
- MessageSourceAutoConfiguration
- Creating my own bean within a @configuration file and autowiring it
- 消息源自动配置
- 在@configuration 文件中创建我自己的 bean 并自动装配它
Below is the MessageSourceAutoConfigurationway:
下面是MessageSourceAutoConfiguration方式:
I am using spring-boot-starter-parent 1.5.7.RELEASE.
我正在使用 spring-boot-starter-parent 1.5.7.RELEASE。
My folder structure:
我的文件夹结构:
DemoApplication.java
演示应用程序.java
@SpringBootApplication
public class DemoApplication
{
public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
}
}
DemoController.java
演示控制器.java
@RestController
public class DemoController implements MessageSourceAware
{
private MessageSource messageSource;
@Override
public void setMessageSource(MessageSource messageSource)
{
this.messageSource = messageSource;
}
@RequestMapping("demo")
public String getLocalisedText()
{
return messageSource.getMessage("test", new Object[0], new Locale("el"));
}
}
Application.yml
应用程序.yml
spring:
messages:
basename: messages
messages.properties
消息.属性
test=This is a demo app!
messages_el.properties
messages_el.properties
test=This is a greek demo app!
pom.xml
pom.xml
<groupId>com.example.i18n.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath />
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
While starting up the server, i can see that the AutoConfiguration works, but cannot find a bean:
在启动服务器时,我可以看到 AutoConfiguration 工作正常,但找不到 bean:
MessageSourceAutoConfiguration matched:
- ResourceBundle found bundle URL [file:/C:/Users/{user}/Downloads/demo/demo/target/classes/messages.properties] (MessageSourceAutoConfiguration.ResourceBundleCondition)
- @ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: current) did not find any beans (OnBeanCondition)
I tried to also explicitly declare my own bean, but didn't work.
我也尝试明确声明我自己的 bean,但没有奏效。
While invoking the endpoint i get the following error:
在调用端点时,我收到以下错误:
{
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.context.NoSuchMessageException",
"message": "No message found under code 'test' for locale 'el'.",
"path": "/demo"
}
The closest SO question i found was this one (2 years old) about a bug in spring which was fixed couple of versions ago: Spring Boot MessageSourceAutoConfiguration
我发现的最接近的 SO 问题是这个(2 岁)关于 spring 中的一个错误,该错误在几个版本前已修复: Spring Boot MessageSourceAutoConfiguration
采纳答案by nicolas
The issue was my Eclipse encoding configuration, which I haven't managed to fix yet.
问题是我的 Eclipse 编码配置,我还没有设法修复。
After debugging Spring's code (ReloadableResourceBundleMessageSource.java
) I could see my key=value
property loaded, but with 3 space characters before each character (e.g. t e s t = T h i s i s a d e m o a p p !
).
在调试 Spring 的代码 ( ReloadableResourceBundleMessageSource.java
) 后,我可以看到我的key=value
属性已加载,但每个字符前有 3 个空格字符(例如t e s t = T h i s i s a d e m o a p p !
)。
On another PC the same demo application works fine.
在另一台 PC 上,相同的演示应用程序运行良好。
回答by ahmetcetin
Can you create a messages package in resources and try this Bean implementation in your configuration file:
你可以在资源中创建一个消息包并在你的配置文件中尝试这个 Bean 实现:
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setCacheSeconds(10); //reload messages every 10 seconds
return messageSource;
}
Additionally, I suggest you to use @Configuration annotated configuration classes instead of xml files to be adapted perfectly to Spring Boot concept.
此外,我建议您使用 @Configuration 注释的配置类而不是 xml 文件以完美适应 Spring Boot 概念。
回答by Aheza Desire
I had the same issue on springboot app. I have tested one of the below options:
我在 springboot 应用程序上遇到了同样的问题。我已经测试了以下选项之一:
If you prefer to modify application.properties
file then add this line spring.messages.basename=messages
where messages is the prefix of the file containing your messages. with this u don't have to setup a messagesource
bean yourself.
如果您更喜欢修改application.properties
文件,请添加此行spring.messages.basename=messages
,其中消息是包含您的消息的文件的前缀。有了这个,你不必messagesource
自己设置一个bean。
or
或者
i had to give the MessageResource
bean a name and autowire it using the name given during initialization, otherwise DelegatingMessageSource
was being injected and it wasn't resolving to any message source.
我必须给MessageResource
bean 一个名称并使用初始化期间给定的名称自动装配它,否则DelegatingMessageSource
被注入并且它不会解析到任何消息源。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localResolver=new SessionLocaleResolver();
localResolver.setDefaultLocale(Locale.US);
return localResolver;
}
@Bean(name = "messageResourceSB")
public MessageSource messageResource() {
ResourceBundleMessageSource messageBundleResrc=new ResourceBundleMessageSource();
messageBundleResrc.setBasename("msg.message");
return messageBundleResrc;
}
}
then autowire the bean with the name u expect
然后使用您期望的名称自动装配 bean
@RestController
public class Internationalization {
@Autowired
@Qualifier("messageResourceSB")
MessageSource messageSource;
@GetMapping(path = "/sayHelloIntern")
public String sayHello(@RequestHeader(name="Accept-Language",required = false) Locale locale) {
return messageSource.getMessage("message.greeting", null, locale);
}
}
回答by Anand
You can also configure through application.properties
也可以通过 application.properties 进行配置
below i18n.messages points to messages files under classpath folder i18n
下面的 i18n.messages 指向类路径文件夹 i18n 下的消息文件
spring.messages.basename=i18n.messages
spring.messages.basename=i18n.messages
Refer SpringBoot doc click here
参考 SpringBoot 文档点击这里