Spring文档苦读【1】

本文详细介绍了 Spring MVC 框架中 BeanFactory 和 ApplicationContext 的区别及应用场景,ApplicationContext 在 BeanFactory 基础上增加了国际化、资源访问等功能,并支持事件发布机制。此外还介绍了 MessageSource 接口的两种实现方式以及 ApplicationContextEvent 的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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.

    •  

转载于:https://www.cnblogs.com/may-cn/p/6043792.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值