java - Spring Boot 应用程序和 MessageSource

标签 java spring-boot

我一直在尝试使用 MessageSource 创建一个演示 spring boot 应用程序,但我无法弄清楚问题出在哪里。我尝试了几种方法:

  • MessageSourceAutoConfiguration
  • 在@configuration 文件中创建我自己的 bean 并 Autowiring 它

下面是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)

我也尝试过显式声明我自己的 bean,但没有成功。

在调用端点时出现以下错误:

{
    "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

最佳答案

你能不能在资源中创建一个消息包并在你的配置文件中尝试这个 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 概念。

关于java - Spring Boot 应用程序和 MessageSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46659679/

相关文章:

java - Spring为什么单次调用@Cacheable注解方法会执行2次@Cacheable keyGenerator

soap - 如何使 JAX-WS 端点接受 SOAP1.2 消息

java - Mapstruct 更新值而不覆盖

java - 在 ubuntu 上启动 Spring boot 应用程序但花费太多时间

java - DropDownChoices 返回 null 作为下拉列表中所选项目的值

java - JPA/Hibernate 加入常量值

java - Spring Boot 让加密问题

java - 如何避免 java 中的重复枚举值?

java - 如何使用 Java(Apache HTTP 客户端)模拟浏览器 HTTPS POST 请求?

java - 如何从 utf-8 表示的字符串中获取源字符串