<Beans>
<Bean id = "messageSource" class = "org. springframework. context. support. ResourceBundleMessageSource">
<Property name = "basename" value = "messages"/>
<Property name = "useCodeAsDefaultMessage" value = "true"/>
</Bean>
</Beans>
Manual encoding injection is now performed to reduce the configuration file:
Public class TestRegisterBean {
Public static void main (String [] args ){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext (
"ApplicationContext. xml"); // you can obtain it in the web environment by using the following method:
// WebApplicationContextUtils. getRequiredWebApplicationContext (servletContextEvent
. GetServletContext ());
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)
ApplicationContext. getBeanFactory ();
GenericBeanDefinition messageSourceDefinition = new GenericBeanDefinition ();
// For parameters, see www.2cto.com in the preceding configuration file.
Map <String, String> original = new HashMap <String, String> ();
Original. put ("basenames", "messages ");
Original. put ("usecodeasdefamessage message", "true ");
MessageSourceDefinition. setBeanClass (ResourceBundleMessageSource. class );
MessageSourceDefinition. setPropertyValues (new MutablePropertyValues (original ));
// Register
BeanFactory. registerBeanDefinition ("ResourceBundleMessageSource ",
MessageSourceDefinition );
// Obtain
ResourceBundleMessageSource messageSource = (ResourceBundleMessageSource) applicationContext
. GetBean ("ResourceBundleMessageSource ");
// Test
System. out. println (messageSource. getMessage ("test", null, null ));
}
}
To test this instance, create messages_zh_CN.properties under classpath and add test.