Spring是常用的开发框架,曾经以为只有2种主要的IOC容器初始化方式

1、显式的实例化ApplicationContext

2、通过web.xml配置

 

以下内容参考自Spring-Reference PDF

显式实例化:

ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");
//MessengerService.class用来确定2个XML文件的路径
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"}, MessengerService.class);
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml");
ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:conf/appContext.xml");

 

web.xml配置

原理基本就是Servlet,摘自Reference PDF

To support the scoping of beans at the request, session, and global session levels
(web-scoped beans), some minor initial configuration is required before you define your beans. (This
initial setup is not required for the standard scopes, singleton and prototype.)
How you accomplish this initial setup depends on your particular Servlet environment..

对于Servlet2.4以上容器且使用了第三方的工具来控制跳转,比如Struts,则必须添加listener

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

 

这个添加是为了scope考量,摘自PDF

If you access scoped beans within Spring Web MVC, in effect, within a request that is processed by the
Spring DispatcherServlet, or DispatcherPortlet, then no special setup is necessary:
DispatcherServlet and DispatcherPortlet already expose all relevant state.

可以看到如果使用MVC,或者纯粹的DispatcherServlet应用等,可无须配置该listener。
If you use a Servlet 2.4+ web container, with requests processed outside of Spring’s DispatcherServlet
(for example, when using JSF or Struts), you need to add the following
javax.servlet.ServletRequestListener to the declarations in your web applications

如果request的处理过程在Dis