<beans>
<bean id=“messageSource” class=“org.springframework.context.support.ResourceBundleMessageSource”>
<property name=“basename” value=“messages”/>
<property name=“useCodeAsDefaultMessage” value=“true”/>
</bean>
</beans>
现在为减少配置文件进行手动编码注入:具体如下
public class TestRegisterBean {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
“applicationContext.xml”);//在web环境下也可通过如下如方式获取
//WebApplicationContextUtils.getRequiredWebApplicationContext(servletContextEvent
.getServletContext());
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)
applicationContext.getBeanFactory();
GenericBeanDefinition messageSourceDefinition = new GenericBeanDefinition();
//参数参考上面配置文件里面
Map<String , String> original = new HashMap<String, String>();
original.put(“basenames”, “messages”);
original.put(“useCodeAsDefaultMessage”, “true”);
messageSourceDefinition.setBeanClass(ResourceBundleMessageSource.class);
messageSourceDefinition.setPropertyValues(new MutablePropertyValues(original));
//注册
beanFactory.registerBeanDefinition(“ResourceBundleMessageSource”,
messageSourceDefinition);
//获取
ResourceBundleMessageSource messageSource = (ResourceBundleMessageSource) applicationContext
.getBean(“ResourceBundleMessageSource”);
//测试
System.out.println(messageSource.getMessage(“test”, null, null));
}
}
测试本实例需在classpath下面创建messages_zh_CN.properties 并添加test.