2017-10-10 155 views
2

我一直在尝试创建一个带有MessageSource的演示弹簧启动应用程序,但我无法弄清楚是什么问题。我试着在几个方面:Spring引导应用程序和MessageSource

  • MessageSourceAutoConfiguration
  • 一个@Configuration文件中创建我自己的豆和自动装配它

下面是MessageSourceAutoConfiguration方式:

我使用spring-boot-starter-parent 1.5.7.RELEASE

我的文件夹结构:

enter image description here

DemoApplication.java

@SpringBootApplication 
public class DemoApplication 
{ 
    public static void main(String[] args) 
    { 
    SpringApplication.run(DemoApplication.class, args); 
    } 
} 

DemoController.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

spring: 
    messages: 
    basename: messages 

messages.properties

test=This is a demo app! 

messages_el.properties

test=This is a greek demo app! 

的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> 

虽然启动服务器,我可以看到,自动配置的作品,却找不到一个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) 

我想也明确声明自己豆,但没”工作。

在调用端点我得到以下错误:

{ 
    "status": 500, 
    "error": "Internal Server Error", 
    "exception": "org.springframework.context.NoSuchMessageException", 
    "message": "No message found under code 'test' for locale 'el'.", 
    "path": "/demo" 
} 

最近的SO问题我发现了这一个(2岁)关于其版本前固定在春天的一个错误: Spring Boot MessageSourceAutoConfiguration

+1

小细节,你的消息文件中'='和翻译的消息之间不应该有空格。 – Thibstars

+1

你把你的'messages.properties'放在哪里?你也可以移除'spring.messages.basename'属性,因为它是默认值。 –

回答

0

问题是我的eclipse编码配置,我还没有设法修复。

调试完spring代码后(ReloadableResourceBundleMessageSource。java)我可以看到我的key = value属性已经加载,但在每个字符之前有3个空格字符(“t s s = T h i s s a d e m o a p p!”)。

在另一台电脑上,同样的演示应用程序工作正常。

3

你可以创建一个信息包中的资源,并尝试在配置文件中这个Bean实现:

@Bean 
public MessageSource messageSource() { 
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); 
    messageSource.setBasename("classpath:messages"); 
    messageSource.setCacheSeconds(10); //reload messages every 10 seconds 
    return messageSource; 
} 

此外,我建议你,而不是使用XML文件@Configuration注解配置类完全适应春天启动概念。

+0

它现在的作品谢谢,这是我的日食问题..我有我的豆这样(仇恨xml):) – nicolas

+0

@nicolas快乐,你发现问题:) – ahmetcetin

相关问题