Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

java - Spring boot application and MessageSource

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:

  • MessageSourceAutoConfiguration
  • Creating my own bean within a @configuration file and autowiring it

Below is the MessageSourceAutoConfiguration way:

I am using 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)

I tried to also explicitly declare my own bean, but didn't work.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Can you create a messages package in resources and try this Bean implementation in your configuration file:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...