Last active
October 13, 2016 22:56
-
-
Save thergbway/5ce67a8c3c182b117838ecd7b9f80131 to your computer and use it in GitHub Desktop.
SPRING. INVERSION OF CONTROL. MESSAGE SOURCE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. ApplicationContext это MessageSource | |
2. Чаще всего текущая реализация MessageSource является HierarchicalMessageSource, как например, ApplicationContext, то | |
ApplicationSource поддерживает идею иерархичности источников сообщений, те если мы не нашли сообщение в одном | |
MessageSource, то идем в другой и ищем там, те по иерархии вверх. | |
3. Возможные реализации MessageSource (точнее HierarchicalMessageSource): | |
* ResourceBundleMessageSource. Использует идеи ResourceBundle из JDK. | |
* ReloadableResourceBundleMessageSource. Более гибкий, можно читать из любого источника, а не только из classpath. | |
также позволяет динамически управлять загруженными сообщениями, например, перезагружать источники. | |
* StaticMessageSource. Позволяет программно создавать сообщения. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
custom_message=Say hello to {0}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> | |
<!--Имя должно быть именно таким--> | |
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> | |
<property name="defaultEncoding" value="UTF-8"/> | |
<property name="basenames"> | |
<list> | |
<value>ioc.message_source.main_messages</value> | |
<value>ioc.message_source.additional_messages</value> | |
</list> | |
</property> | |
</bean> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ioc.message_source; | |
import org.springframework.context.MessageSource; | |
import org.springframework.context.support.ClassPathXmlApplicationContext; | |
import java.util.Locale; | |
public class Main { | |
public static void main(String[] args) { | |
ClassPathXmlApplicationContext cx = new ClassPathXmlApplicationContext("ioc/message_source/context.xml"); | |
MessageSource messageSource = cx;//Note! Our context is MessageSource | |
String m1 = messageSource.getMessage("custom_message", new Object[]{"User"}, "Default_User", null); | |
String m2 = messageSource.getMessage("program.about", null, null); | |
String m3 = messageSource.getMessage("program.about", null, Locale.forLanguageTag("fr-FR")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program.about=This program is awesome! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program.about=Programme très cool! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment