</pre><pre class="java" name="code">創建監聽器和ServletContext的code:
</pre><pre class="java" name="code"><context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener的作用(一句話):初始化BeanFactory,並將BeanFactory設置到application中。
說明:
創建了ContextLoaderListener這個監聽器,它繼承了ContextLoader類、實現了ServletContextListener接口,監聽器對Application的創建進行了監聽 ;
有ServletContext創建了,這個事件就會被監聽器的contextInitlized(ServletContext event)方法監聽到;
將application設置到contextLoader屬性上;
執行源碼的this.contextLoader.initWebApplicationContext(event.getServletContext());
取出web.xml中contextConfigLocation參數的值,也就是spring的配置信息;
根據這些配置信息生成Bean工廠;
最後把這個bean工廠設置到application中去;
可以在後端處理器(Action)中通過application取出beanFactory,進而從beanFactory取出業務對象,進行業務操作。
--------------------------------------------------------------------------------
在spring、springMVC,項目中這種配置,具有通用性。