|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.context;
|
18 | 18 |
|
19 |
| -import java.nio.charset.Charset; |
20 |
| - |
21 | 19 | import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
22 | 20 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
23 | 21 | import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
|
53 | 51 | @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
|
54 | 52 | @Conditional(ResourceBundleCondition.class)
|
55 | 53 | @EnableConfigurationProperties
|
56 |
| -@ConfigurationProperties(prefix = "spring.messages") |
57 | 54 | public class MessageSourceAutoConfiguration {
|
58 | 55 |
|
59 | 56 | private static final Resource[] NO_RESOURCES = {};
|
60 | 57 |
|
61 |
| - /** |
62 |
| - * Comma-separated list of basenames, each following the ResourceBundle convention. |
63 |
| - * Essentially a fully-qualified classpath location. If it doesn't contain a package |
64 |
| - * qualifier (such as "org.mypackage"), it will be resolved from the classpath root. |
65 |
| - */ |
66 |
| - private String basename = "messages"; |
67 |
| - |
68 |
| - /** |
69 |
| - * Message bundles encoding. |
70 |
| - */ |
71 |
| - private Charset encoding = Charset.forName("UTF-8"); |
72 |
| - |
73 |
| - /** |
74 |
| - * Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles |
75 |
| - * are cached forever. |
76 |
| - */ |
77 |
| - private int cacheSeconds = -1; |
78 |
| - |
79 |
| - /** |
80 |
| - * Set whether to fall back to the system Locale if no files for a specific Locale |
81 |
| - * have been found. if this is turned off, the only fallback will be the default file |
82 |
| - * (e.g. "messages.properties" for basename "messages"). |
83 |
| - */ |
84 |
| - private boolean fallbackToSystemLocale = true; |
85 |
| - |
86 |
| - /** |
87 |
| - * Set whether to always apply the MessageFormat rules, parsing even messages without |
88 |
| - * arguments. |
89 |
| - */ |
90 |
| - private boolean alwaysUseMessageFormat = false; |
| 58 | + @Bean |
| 59 | + @ConfigurationProperties(prefix = "spring.messages") |
| 60 | + public MessageSourceProperties messageSourceProperties() { |
| 61 | + return new MessageSourceProperties(); |
| 62 | + } |
91 | 63 |
|
92 | 64 | @Bean
|
93 | 65 | public MessageSource messageSource() {
|
| 66 | + MessageSourceProperties properties = messageSourceProperties(); |
94 | 67 | ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
95 |
| - if (StringUtils.hasText(this.basename)) { |
| 68 | + if (StringUtils.hasText(properties.getBasename())) { |
96 | 69 | messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
|
97 |
| - StringUtils.trimAllWhitespace(this.basename))); |
| 70 | + StringUtils.trimAllWhitespace(properties.getBasename()))); |
98 | 71 | }
|
99 |
| - if (this.encoding != null) { |
100 |
| - messageSource.setDefaultEncoding(this.encoding.name()); |
| 72 | + if (properties.getEncoding() != null) { |
| 73 | + messageSource.setDefaultEncoding(properties.getEncoding().name()); |
101 | 74 | }
|
102 |
| - messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale); |
103 |
| - messageSource.setCacheSeconds(this.cacheSeconds); |
104 |
| - messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat); |
| 75 | + messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale()); |
| 76 | + messageSource.setCacheSeconds(properties.getCacheSeconds()); |
| 77 | + messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat()); |
105 | 78 | return messageSource;
|
106 | 79 | }
|
107 | 80 |
|
108 |
| - public String getBasename() { |
109 |
| - return this.basename; |
110 |
| - } |
111 |
| - |
112 |
| - public void setBasename(String basename) { |
113 |
| - this.basename = basename; |
114 |
| - } |
115 |
| - |
116 |
| - public Charset getEncoding() { |
117 |
| - return this.encoding; |
118 |
| - } |
119 |
| - |
120 |
| - public void setEncoding(Charset encoding) { |
121 |
| - this.encoding = encoding; |
122 |
| - } |
123 |
| - |
124 |
| - public int getCacheSeconds() { |
125 |
| - return this.cacheSeconds; |
126 |
| - } |
127 |
| - |
128 |
| - public void setCacheSeconds(int cacheSeconds) { |
129 |
| - this.cacheSeconds = cacheSeconds; |
130 |
| - } |
131 |
| - |
132 |
| - public boolean isFallbackToSystemLocale() { |
133 |
| - return this.fallbackToSystemLocale; |
134 |
| - } |
135 |
| - |
136 |
| - public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) { |
137 |
| - this.fallbackToSystemLocale = fallbackToSystemLocale; |
138 |
| - } |
139 |
| - |
140 |
| - public boolean isAlwaysUseMessageFormat() { |
141 |
| - return this.alwaysUseMessageFormat; |
142 |
| - } |
143 |
| - |
144 |
| - public void setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat) { |
145 |
| - this.alwaysUseMessageFormat = alwaysUseMessageFormat; |
146 |
| - } |
147 |
| - |
148 | 81 | protected static class ResourceBundleCondition extends SpringBootCondition {
|
149 | 82 |
|
150 | 83 | private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<>();
|
|
0 commit comments