我已经宣布了一个组件
@Component
public class I18nManagerImpl implements I18nManager{
@Autowired
MessageSource messageSource;
public ResourceBundle getResourceBundle() {
Locale fromConfig = Locale.UK;
//Resolve Locale from a configuration service
return new MessageSourceResourceBundle(messageSource, fromConfig); //replace code in my question
}
@Override
public String message(String locale) {
ResourceBundle beanResourceBundle = getResourceBundle();
String invalidEmail = beanResourceBundle.getString("invalidEmail");
return invalidEmail;
}
}
messageSource
中的applicationContext.xml
bean定义是
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames" value="messages"/>
</bean>
但无法找到消息,如果使用调试器查看getResourceBundle键返回的ResourceBundle为null且没有内容。
提供的代码有什么问题? 以下是项目结构的图像:
答案 0 :(得分:0)
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames" value="classpath:messages"/>
</bean>
添加classpath
另外添加 -
<resources mapping="/resources/**" location="/resources/" />
由于您使用的是注释 - 添加
<annotation-driven />
<context:component-scan base-package="your.base.package" />
资源的命名空间 -
<beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
答案 1 :(得分:0)
问题是propoerties文件不在源根目录中。我将文件插入src config文件夹,并将该文件夹标记为源根目录。