Ê×Ò³ Ê«´Ê ×Öµä °å±¨ ¾ä×Ó ÃûÑÔ ÓÑ´ð ÀøÖ¾ ѧУ ÍøÕ¾µØÍ¼
µ±Ç°Î»ÖÃ: Ê×Ò³ > ½Ì³ÌƵµÀ > Èí¼þ¹ÜÀí > Èí¼þ¼Ü¹¹Éè¼Æ >

spring ¹ú¼Ê»¯£¨¶þ£©

2013-03-22 
spring ¹ú¼Ê»¯£¨2£©´ó¼Ò¿´ÁËÎÒдµÄspring ¹ú¼Ê»¯£¨1£©ºó£¬Ó¦¸Ã»á·¢ÏÖÈç¹ûÒª»ñÈ¡×ÊÔ´ÎļþµÄÐÅϢʱ£¬ÐèÒª½«ÅäÖÃÎÄ

spring ¹ú¼Ê»¯£¨2£©

´ó¼Ò¿´ÁËÎÒдµÄspring ¹ú¼Ê»¯£¨1£©ºó£¬Ó¦¸Ã»á·¢ÏÖÈç¹ûÒª»ñÈ¡×ÊÔ´ÎļþµÄÐÅϢʱ£¬ÐèÒª½«ÅäÖÃÎļþÖеÄ×ÊÔ´Îļþbean×¢Èëµ½ÄãÕýÔÚ²Ù×÷µÄbeanÒԺ󣬲ÅÄÜʹÓá£

springµÄ×ö·¨Êǽ«¹ú¼Ê»¯×ÊÔ´×÷³Éϵͳ¼¶±ðµÄ£¬¶ÔÈκÎ×é¼þ¶¼½øÐпª·Å¡£

ʵÏÖ·½·¨ÈçÏ£º

org.springframework.context.ApplicationContextÕâ¸ö½Ó¿ÚÒ²¼Ì³ÐÁËorg.springframework.context.MessageSource½Ó¿Ú

springÈÝÆ÷ÔÚÆô¶¯Ê±»áµ÷ÓÃinitMessageSource£¨£©·½·¨£¬ËüµÄ×÷ÓþÍÊdzõʼ»¯ÈÝÆ÷ÖеĹú¼Ê»¯×ÊÔ´¡£

¸ù¾Ý·´Éä»úÖÆ£¬´ÓBeanDefinitionRegistryÖÐÕÒµ½ÃûΪ¡°messageSource¡±²¢ÇÒÀàÐÍΪorg.springframework.context.MessageSourceµÄbean,½«Ëü¼ÓÔØÎªÈÝÆ÷¼¶µÄ¹ú¼Ê»¯×ÊÔ´Îļþ¡£

?

ÀýÈ磺ÏÂÃæµÄxml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:p="http://www.springframework.org/schema/p"       xsi:schemaLocation="http://www.springframework.org/schema/beans                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">    <!-- Ãû³Ç±ØÐëΪmessageSource£¬ ÀàÐͱØÐëΪorg.springframework.context.MessageSource -->    <!-- classΪResourceBundleMessageSource»òReloadableResourceBundleMessageSource¶¼¿ÉÒÔ£¨Çø±ðÒѱȽϹý£© -->    <bean id="messageSource" name="code">package spring3.local;import java.util.Locale;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MessageResourceMain {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("spring3/local/message_resource.xml");System.out.println("CHINA:"+ context.getMessage("greeting.morning", null, Locale.CHINA));System.out.println("JAPAN:"+ context.getMessage("greeting.morning", null, Locale.JAPAN));System.out.println("US:"+ context.getMessage("greeting.morning", null, Locale.US));System.out.println("CANADA:"+ context.getMessage("greeting.morning", null, Locale.CANADA));}}

?Êä³ö½á¹û

CHINA:ÔçÉϺÃJAPAN:¤ªÔ礦¤´¤¶¤¤¤Þ¤¹US:good night!CANADA:¤ªÔ礦¤´¤¶¤¤¤Þ¤¹

?

ÈȵãÅÅÐÐ