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
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
The --debug
option prints auto-configure settings, which makes it easier to understand the situation.
In this case,
MessageSource
components (MessageSourceImpl
) are implemented on their own, so MessageSourceAutoConfiguration
does not apply
new
myself)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.
© 2024 OneMinuteCode. All rights reserved.