首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring学习札记(一)——Spring IOC控制反转

2012-09-17 
Spring学习笔记(一)——Spring IOC控制反转!-- Listener contextConfigLocation --listenerlistener-cl

Spring学习笔记(一)——Spring IOC控制反转
<!-- Listener contextConfigLocation --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
对于实现规范servlet2.2的web容器,只能使用ContextLoaderServlet
4.2)ApplicationContextAware
在Spring应用中,有时需要将Spring应用中的ApplicationContext实例注入到JavaBean实例中。例如:为了在某Javabean实例中动态获得ApplicationContext创建的某单例JavaBean,但是该单例JavaBean并没有显式的使用(在Spring配置文件中没有显式地给出对单例JavaBean的引用)到它。这种情况下,借助ApplicationContextAware能实现开发要求。如:
org.springframework.context.ApplicationContextAware
其中定义了如下方法:
void setApplicationContext(ApplicationContext applicationContext)
?????????????????????????? throws BeansException
比如在Spring配置中定义了两个JavaBean:bean1和bean2,如果bean2需要使用到bean1,但是bean2的Spring配置中,没有显式的配置对bean1的引用(没有<ref>)。此时,借助ApplicationContextAware,让bean2实现ApplicationContextAware接口,定义私有的applicationContext变量,并提供相应的setter方法。通过调用applicationContext的getBean方法就可以获取需要的bean1。
这同BeanFactoryAware 类似。
4.3)ApplicationContext接口实现
基于ApplicationContext接口,Spring框架提供了若干实现。
a)ClassPathXmlApplicationContext:在web应用中,开发者可以从classpath中,即WEB-INF/classes或WEB-INF/lib的jar中装载Spring配置文件。单元测试中常用到。
b)FileSystemXmlApplicationContext:开发者可以从文件系统中装载Spring配置文件。单元测试常用。
c)XmlWebApplicationContext:供ContextLoaderListener或ContextLoaderServlet内部装载Spring配置文件使用。

热点排行