SpringBoot+ Multi-Module Configuration Cannot Retrieve Messages from Property Files

Asked 2 years ago, Updated 2 years ago, 112 views

In the SpringBoot project in a multi-module configuration
Creating a login screen using SpringSecurity


when UsernameNotFoundException occurs on the domain layer side
on the domain layer side I'd like to get a message from /resources/i18n/messages_ja.properties.

No message found under code 'test.error' for locale'ja_JP'.
is displayed and cannot be retrieved

■Determination processing part

@Service
public classUserDetailsServiceImplementationsUserDetailsService{

    @Autowired
    MessageSourceImpl message;

    @ Override
    publicUserDetailsloadUserByUsername(String email)throwsUsernameNotFoundException{

        if(!email.equals("aaaa")}
            through new UsernameNotFoundException(message.getMessage("test.error"));
        }

        return null;
    }

}


If you write it in the messages.properties under /resources, it will be retrieved correctly. I think it's around the setting, but even after more than a week of trial and error, it doesn't work at all.
I would appreciate it if you could help me.I really appreciate your cooperation.

■ Total amount of sauce
https://github.com/hi-soft68/hisohi

I am sorry if there is a shortage in my first post.

java spring-boot

2022-09-30 21:28

2 Answers

This is a quick answer (I'm sorry if I made a mistake).

Is application.yml not required in hisohi/test-domain/src/main/resources/?

Since there is no application.yml, I thought that the following definition might not work and the Japanese message could not be retrieved.

 spring:
  messages:
    baseline —i18n/messages


2022-09-30 21:28

The --debug option prints auto-configure settings, which makes it easier to understand the situation.

In this case,

You must build your own MessageSource.

 diff -- gita/test-domain/src/main/java/com/example/test/domain/service/MessageSourceImpl.java b/test-domain/src/main/java/com/example/test/domain/service/MessageSourceImpl.java
index35113d6..5f46f48100644
--- a/test-domain/src/main/java/com/example/test/domain/service/MessageSourceImpl.java
+++ b/test-domain/src/main/java/com/example/test/domain/service/MessageSourceImpl.java
@@ - 2,16+2,28@@ package com.example.test.domain.service;

 import java.util.Locale;

+ import javax.announcement.PostConstruct;
+
+ import org.springframework.beans.factory.announcement.Autowired;
 import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration;
+ import org.springframework.boot.autoconfigure.context.MessageSourceProperties;
 import org.springframework.context.MessageSource;
 import org.springframework.context.MessageSourceResolve;
 import org.springframework.context.NoSuchMessageException;
+ import org.springframework.context.support.ResourceBundleMessageSource;
 import org.springframework.stereotype.Component;

 @Component
 public class MessageSourceImplementationsMessageSource{

-    private MessageSource message=new MessageSourceAutoConfiguration().messageSource();
+    private MessageSource message;
+
+    @ PostConstruct
+    public void init() {
+        ResourceBundleMessageSource=new ResourceBundleMessageSource();
+        messageSource.setBasename("i18n/messages");
+        message=messageSource;
+    }

     public String getMessage (String code) {
         return message.getMessage(code, null, Locale.getDefault());

If you want to read the "i18n/messages" part from the configuration file, you can implement MessageSourceProperties equivalent yourself.

Also, the latest version seems to have changed the implementation around here, so the behavior may be different.


2022-09-30 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.