|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.context;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
| 20 | +import java.io.UncheckedIOException; |
19 | 21 | import java.time.Duration;
|
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | +import java.util.stream.Collectors; |
20 | 25 |
|
21 | 26 | import org.springframework.aot.hint.RuntimeHints;
|
22 | 27 | import org.springframework.aot.hint.RuntimeHintsRegistrar;
|
| 28 | +import org.springframework.beans.factory.config.PropertiesFactoryBean; |
23 | 29 | import org.springframework.boot.autoconfigure.AutoConfiguration;
|
24 | 30 | import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
25 | 31 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
39 | 45 | import org.springframework.context.support.AbstractApplicationContext;
|
40 | 46 | import org.springframework.context.support.ResourceBundleMessageSource;
|
41 | 47 | import org.springframework.core.Ordered;
|
| 48 | +import org.springframework.core.io.DefaultResourceLoader; |
42 | 49 | import org.springframework.core.io.Resource;
|
| 50 | +import org.springframework.core.io.ResourceLoader; |
43 | 51 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
44 | 52 | import org.springframework.core.type.AnnotatedTypeMetadata;
|
45 | 53 | import org.springframework.util.ConcurrentReferenceHashMap;
|
@@ -81,6 +89,25 @@ public MessageSource messageSource(MessageSourceProperties properties) {
|
81 | 89 | }
|
82 | 90 | messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
|
83 | 91 | messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
|
| 92 | + |
| 93 | + try { |
| 94 | + if (StringUtils.hasText(properties.getCommonMessages())) { |
| 95 | + PropertiesFactoryBean propertiesFactory = new PropertiesFactoryBean(); |
| 96 | + ResourceLoader resourceLoader = new DefaultResourceLoader(); |
| 97 | + String[] commonMessages = StringUtils.commaDelimitedListToStringArray( |
| 98 | + StringUtils.trimAllWhitespace(properties.getCommonMessages())); |
| 99 | + List<Resource> commonResources = Arrays.stream(commonMessages) |
| 100 | + .map(resourceLoader::getResource) |
| 101 | + .toList(); |
| 102 | + propertiesFactory.setLocations(commonResources.toArray(Resource[]::new)); |
| 103 | + propertiesFactory.setSingleton(true); |
| 104 | + propertiesFactory.setIgnoreResourceNotFound(true); |
| 105 | + propertiesFactory.afterPropertiesSet(); |
| 106 | + messageSource.setCommonMessages(propertiesFactory.getObject()); |
| 107 | + } |
| 108 | + } catch (IOException e) { |
| 109 | + throw new UncheckedIOException("Failed to load common messages", e); |
| 110 | + } |
84 | 111 | return messageSource;
|
85 | 112 | }
|
86 | 113 |
|
|
0 commit comments