I have problem to make "database-driven resource bundle" work. In example below TextDAO
is properly injected during application start, but when messageSource
is accessed, a new Messages
object is created - that's the point. How to make this work ?
<!-- message source -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="someapp.bundle.Messages" />
</bean>
Messages.java
@Component
public class Messages extends ListResourceBundle {
@Autowired
private TextDAO textDAO;
public Messages() {
log.debug("CONSTRUCTOR");
}
@Override
protected Object[][] getContents() {
// loading messages from DB
List<Text> texts = textDAO.findAll(); // textDAO is null
...
}
}
As Bozho
suggest i did my resource bundle as below, but have problem to dynamically reload it. I suppose that ReloadableResourceBundleMessageSource is for properties files, but maybe it is possible to work this, too.
public class DatabaseDrivenMessageSource extends ReloadableResourceBundleMessageSource {
private Logger log = LoggerFactory.getLogger(getClass());
private final Map<String, Map<String, String>> properties = new HashMap<String, Map<String, String>>();
private TextDAO textDAO;
@Autowired
public DatabaseDrivenMessageSource(TextDAO textDAO) {
this.textDAO = textDAO;
reload();
}
@Override
protected MessageFormat resolveCode(String code, Locale locale) {
String msg = getText(code, locale);
MessageFormat result = createMessageFormat(msg, locale);
return result;
}
@Override
protected String resolveCodeWithoutArguments(String code, Locale locale) {
return getText(code, locale);
}
private String getText(String code, Locale locale) {
Map<String, String> localized = properties.get(code);
String textForCurrentLanguage = null;
if (localized != null) {
textForCurrentLanguage = localized.get(locale.getLanguage());
if (textForCurrentLanguage == null) {
textForCurrentLanguage = localized.get(Locale.ENGLISH.getLanguage());
}
}
return textForCurrentLanguage != null ? textForCurrentLanguage : code;
}
public void reload() {
properties.clear();
properties.putAll(loadTexts());
}
protected Map<String, Map<String, String>> loadTexts() {
log.debug("loadTexts");
Map<String, Map<String, String>> m = new HashMap<String, Map<String, String>>();
List<Text> texts = textDAO.findAll();
for(Text text: texts) {
Map<String, String> v = new HashMap<String, String>();
v.put("en", text.getEn());
v.put("de", text.getDe());
m.put(text.getKey(), v);
}
return m;
}
}
Spring's application context is able to resolve text messages for a target locale by their keys. Typically, the messages for one locale should be stored in one separate properties file. This properties file is called a resource bundle. MessageSource is an interface that defines several methods for resolving messages.
The ResourceBundleMessageSource is the implementation of MessageSource interface which is used for resolving messages. 2. This message source caches both the accessed ResourceBundle instances and the generated MessageFormats for each message.
basename
is a string, so it is not a spring bean, and hence no injection there.
What you can try to do is to subclass ReloadableResourceBundleMessageSource
and override some methods there (for example - getMessage(..)
). The DAO should be injected in the subclass.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With