Spring文档苦读【1】

  spring mvc中BeanFactory提供了一些基本的管理和操作Bean的功能,而ApplicationContext继承了BeanFactory,

在其基础上增加了一些功能,如

    • 允许访问国际化,实现了MessageSource
    • 允许访问资源,如URL,files等,实现了ResourceLoader 
    • 实现了事件发布机制,即观察者模式的具体实现。实现了ApplicationEventPublisher 
    • 具体啥意思不是很清楚,应该是多样化的容器,允许每个集中在特定的曾,如Web层
      • Loading of multiple (hierarchical) contexts, allowing each to be focused on one particular layer, such as the web layer of an application, through the HierarchicalBeanFactory interface.

 

MessageSource 实现

  MessageSource提供了两种实现方式,ResourceBundleMessageSource 和 StaticMessageSource。这两个都继承了HierarchicalMessageSource接口。

  StaticMessageSource 

    很少使用,但是提供了编程的方式,往文件中添加内容。

  ResourceBundleMessageSource 

    使用方式如下:beans.xml配置

 1 <beans>
 2     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
 3         <property name="basenames">
 4             <list>
 5                 <value>format</value>
 6                 <value>exceptions</value>
 7                 <value>windows</value>
 8             </list>
 9         </property>
10     </bean>
11 </beans>

    此处标识,在你的项目根路径下,有三个properties文件,名字为format.properties,exceptions.properties,windows.properties

 

# in format.properties
message=Alligators rock!
# in exceptions.properties
argument.required=The {0} argument is required.
public static void main(String[] args) {
    MessageSource resources = new ClassPathXmlApplicationContext("beans.xml");
    String message = resources.getMessage("message", null, "Default", null);
    System.out.println(message);
}

java调用方式如上,输出:Alligators rock!

ApplicationContextEvent

  系统默认实现类

  

EventExplanation

ContextRefreshedEvent

Published when the ApplicationContext is initialized or refreshed, for example, using the refresh() method on theConfigurableApplicationContext interface. "Initialized" here means that all beans are loaded, post-processor beans are detected and activated, singletons are pre-instantiated, and the ApplicationContext object is ready for use. As long as the context has not been closed, a refresh can be triggered multiple times, provided that the chosen ApplicationContext actually supports such "hot" refreshes. For example, XmlWebApplicationContext supports hot refreshes, butGenericApplicationContext does not.

ContextStartedEvent

Published when the ApplicationContext is started, using the start() method on the ConfigurableApplicationContextinterface. "Started" here means that all Lifecycle beans receive an explicit start signal. Typically this signal is used to restart beans after an explicit stop, but it may also be used to start components that have not been configured for autostart , for example, components that have not already started on initialization.

ContextStoppedEvent

Published when the ApplicationContext is stopped, using the stop() method on the ConfigurableApplicationContextinterface. "Stopped" here means that all Lifecycle beans receive an explicit stop signal. A stopped context may be restarted through a start() call.

ContextClosedEvent

Published when the ApplicationContext is closed, using the close() method on the ConfigurableApplicationContextinterface. "Closed" here means that all singleton beans are destroyed. A closed context reaches its end of life; it cannot be refreshed or restarted.

RequestHandledEvent

A web-specific event telling all beans that an HTTP request has been serviced. This event is published after the request is complete. This event is only applicable to web applications using Spring’s DispatcherServlet.

       

posted on 2016-11-09 09:31  源码解析  阅读(233)  评论(0编辑  收藏  举报

导航