多个上下文
我们可以设置多个上下文,这些上下文是树状继承结构。即包括管理自己的bean,父辈的bean,祖辈的bean,以此类推。我们可以使用一些公共的bean,也可以使用一些不同的隔离的bean。例如对某个web应用,普通用户和管理员用户,他们有不同也有相同的处理。
XML的配置
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app ......>
... ...
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/resource/*</url-pattern>
</servlet-mapping>
<!-- 设置一个根上下文root context -->
<!-- 本例具有一个Root上下文和一个(或者多个)Servlet上下文。-->
<!-- 1)每个Servlet上下文对应不同的DispatcherServlets,他们之间相互隔离,因此通常会有一个root context来共享一些共用的bean(如业务逻辑处理,数据获取等等)。-->
<!-- 2)root context是所有的DispatcherServlets的parent,由ContextLoaderListener所创建。同样root context有servletContext,但由于其不输入任何具体的servlet,没有ServletConfig -->
<!-- 3)ContextLoaderListener是在ServletContextListener中实现的,是在web app一启动时调用,在任何serlvet启动之前,即在springDispatcher之前,将load入由contextConfigLocation属性定义的的配置文件。注意这个contextConfigLocation和DispatcherSerlvet中的contextConfigLocation的不同,它们不会混淆 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/rootContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置一个或者多个Servlet context -->
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servletContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
根上下文配置:rootContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 由于只涉及到bean的定义,不设置其他,所以schema只需要beans,其实我们可以将bean,mvc和context都加上 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean name="greetingServiceImpl" class="cn.wei.flowingflying.chapter12.GreetingServiceImpl" />
</beans>
servlet上下文设置 servletContext.xml
我们已经在root中设置了greetingServiceImpl bean,servlet context会继承,因此不能重复设置。由于涉及context的继承,因此和单一context的设置相比,schema增加了context。而Controller属于MVC,因此也必然有mvc的schema。<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<mvc:annotation-driven />
<bean name="helloController" class="cn.wei.flowingflying.chapter12.HelloController">
<property name="greetingService" ref="greetingServiceImpl"/>
</bean>
</beans>
相关链接: 我的Professional Java for Web Applications相关文章