Spring IOC官方文档学习笔记(十四)之ApplicationContext的其他功能 ...

打印 上一主题 下一主题

主题 1921|帖子 1921|积分 5763

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.使用MessageSource
(1) 有时,我们的项目可能会面临国际化需求,例如:对不同国家的人,我们需返回不同语言的消息,而java本身已经给我们提供了ResourceBundle类实现国际化的需求,如下
  1. //在resources目录下,新建两个配置文件,分别为message_en_us.properties和message_zh_cn.properties,内容如下
  2. //message_en_us.properties文件中配置如下
  3. country=us
  4. //message_zh_cn.properties文件中配置如下,注意对中文使用unicode编码
  5. country=\u4e2d\u56fd
  6. //现在,我们希望我们的项目在不同的国家返回不同的country信息,那么就可以使用ResourceBundle类了,如下
  7. public static void main(String[] args) {
  8.     //使用ResourceBundle加载的文件都必须放置在resources根目录下,因此我们的message_en_us.properties和message_zh_cn.properties文件都位于resources根目录,而且这些文件都必须按照${name}_${language}_${region}的方式来命名,因为这种命名方式正好能对应ResourceBundle.getBundle()方法中的参数,例如ResourceBundle.getBundle("message", new Locale("zh", "cn")),其中,message对应${name},zh对应${language},cn对应${region},即ResourceBundle.getBundle("message", new Locale("zh", "cn"))这个方法会读取我们的message_zh_cn.properties配置文件,这样我们就可以根据不同的参数来读取不同的文件,达到国际化的目的
  9.     //未指定它的Locale,因此java获取它当前所在的地区,为cn
  10.     ResourceBundle DefaultBundle = ResourceBundle.getBundle("message");
  11.     System.out.println(DefaultBundle.getString("country"));
  12.     //指定地区为cn
  13.     ResourceBundle cnBundle = ResourceBundle.getBundle("message", new Locale("zh","cn"));
  14.     System.out.println(cnBundle.getString("country"));
  15.     //指定地区为us
  16.     ResourceBundle uSbundle = ResourceBundle.getBundle("message", new Locale("en","us"));
  17.     System.out.println(uSbundle.getString("country"));
  18. }
  19. //打印结果如下,通过ResourceBundle实现了国际化
  20. 中国
  21. 中国
  22. us
复制代码
(2) Spring提供了MessageSource来帮助我们实现国际化功能,具体的使用方法同jdk中的ResourceBundle,如下
  1. //在resources目录下,新建两个配置文件,分别为message_en.properties和message_zh.properties,内容如下
  2. //message_en.properties文件中配置如下
  3. country=us
  4. //message_zh.properties文件中配置如下
  5. country=中国
  6. <beans ....>
  7.         
  8.         <bean id="messageSource"
  9.               >
  10.             <property name="basenames">
  11.                 <list>
  12.                     
  13.                     <value>message</value>
  14.                 </list>
  15.             </property>
  16.             
  17.             <property name="defaultEncoding">
  18.                 <value>UTF-8</value>
  19.             </property>
  20.         </bean>
  21. </beans>
  22. //ApplicationContext继承了MessageSource接口
  23. MessageSource messageSource = new ClassPathXmlApplicationContext("beans.xml");
  24. //指定不同的语言,来获取不同消息
  25. String zh = messageSource.getMessage("country", null, Locale.CHINESE);
  26. System.out.println(zh);
  27. String en = messageSource.getMessage("country", null, Locale.ENGLISH);
  28. System.out.println(en);
  29. //启动容器,输出如下
  30. 中国
  31. us
复制代码
(3) Spring的MessageSource还提供了占位符功能,来进行消息内容的填充,如下例所示
  1. //向message_zh.properties中添加配置项如下,{0}表示第一个占位符,还有{1},{2}等等,以此类推
  2. argument=we need {0}
  3. //main函数
  4. MessageSource messageSource = new ClassPathXmlApplicationContext("beans.xml");
  5.                                                      //Object[]指定向占位符填充的内容
  6. String argument = messageSource.getMessage("argument", new Object[]{"蛋糕"}, Locale.CHINESE);
  7. System.out.println(argument);
  8. //启动后,打印如下
  9. we need 蛋糕
复制代码
2.标准和自定义事件
(1) Spring中的事件是通过ApplicationEvent类和ApplicationListener接口提供的,如果一个bean实现了ApplicationListener接口,那么每当一个ApplicationEvent发布到Spring中时,都会通知该bean
(2) Spring中内置事件
事件说明ContextRefreshedEvent容器初始化或刷新时(refresh)时发布该事件ContextStartedEvent通过调用ConfigurationApplicationContext接口中的start()方法启动容器时发布该事件ContextStoppedEvent通过调用ConfigurationApplicationContext接口中的stop()方法停止容器时发布该事件ContextClosedEvent通过调用ConfigurationApplicationContext接口中的close()方法或jvm关闭钩子关闭容器时发布该事件RequestHandledEvent适用于使用了DispatcherServlet的web环境中,在请求完成后发布该事件,用于告知所有的bean已经为http请求提供了服务ServletRequestHandledEventRequestHandledEvent的子类,其中添加了Servlet特定信息(3) 示例如下
  1. //现在假设有一个用户注册事件,每当一个用户注册后,进行相应的其他操作(如发送邮件等等)
  2. //自定义事件,需继承ApplicationEvent
  3. public class RegisterEvent extends ApplicationEvent {
  4.     private String username;
  5.     public RegisterEvent(Object source,String username) {
  6.         super(source);
  7.         this.username = username;
  8.     }
  9.     public String getUsername() {
  10.         return username;
  11.     }
  12. }
  13. //使用ApplicationEventPublisher中的publishEvent()方法来向容器中发布一个事件
  14. @Service
  15. public class RegisterService implements ApplicationEventPublisherAware {
  16.     private ApplicationEventPublisher publisher;
  17.     @Override
  18.     public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
  19.         this.publisher = applicationEventPublisher;
  20.     }
  21.     //publishEvent()方法会阻塞,直到所有的监听器都完成了对事件的处理
  22.     public void finishRegister(String username) {
  23.         this.publisher.publishEvent(new RegisterEvent(this, username));
  24.     }
  25. }
  26. //实现ApplicationListener接口,实现某种类型事件的监听者
  27. @Component
  28. public class RegisterLister implements ApplicationListener<RegisterEvent> {
  29.     //每当有一个RegisterEvent事件发布后,都会触发该回调
  30.     @Override
  31.     public void onApplicationEvent(RegisterEvent registerEvent) {
  32.         System.out.println("用户:" + registerEvent.getUsername() + "完成注册...");
  33.         //do other things,such as send emails
  34.     }
  35. }
  36. //main
  37. ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext("cn.example.spring");
  38. String username ="zpc";
  39. ctx.getBean(RegisterService.class).finishRegister(username);
  40. //启动后,容器打印如下,可见Spring使用了观察者模式,来实现了一个事件发布与订阅的功能
  41. 用户:zpc完成注册...
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

钜形不锈钢水箱

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表