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
系统默认实现类
Event | Explanation |
---|---|
|
Published when the |
|
Published when the |
|
Published when the |
|
Published when the |
|
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 |