首页 文章

Spring启动应用程序和MessageSource

提问于
浏览
9

我一直在尝试使用MessageSource创建一个演示 spring 启动应用程序,但我无法弄清楚是什么问题 . 我试过几种方式:

  • MessageSourceAutoConfiguration

  • 在@configuration文件中创建自己的bean并自动装配它

以下是 MessageSourceAutoConfiguration 方式:

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

My folder structure:

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>

While starting up the server, i can see that the AutoConfiguration works, but cannot find a 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)

我试图也明确声明我自己的bean,但没有用 .

在调用 endpoints 时,我收到以下错误:

{
    "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 天的一个错误,修复了几个版本之前:Spring Boot MessageSourceAutoConfiguration

2 回答

  • 0

    您可以在资源中创建消息包,并在配置文件中尝试此Bean实现:

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

    另外,我建议你使用@Configuration带注释的配置类而不是xml文件来完全适应Spring Boot概念 .

  • 19

    问题是我的eclipse编码配置,我还没有设法解决 .

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

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

相关问题