Spring是一个容器,SpringMVC也是一个容器,这就出现问题啦,我Spring和SpringMVC一起在WEB项目使用的时候如何创建容器,创建那个容器,或者两个容器分别有一些什么配置参数,这两个容器时什么样的关系。
Spring是父容器,SpringMVC是子容器,子容器可以调用父容器中的bean,但是父容器不可以调用子容器中的bean
在 Spring MVC 配置文件中引用业务层的 Bean多个 Spring IOC 容器之间可以设置为父子关系,以实现良好的解耦。Spring MVC WEB 层容器可作为 “业务层” Spring 容器的子容器dao ,service,handler,数据源,声明式事务,拦截器,包扫描,视图解析器,文件下载的配置... spring.xml: dao,service,数据源,声明式事务,包扫描 springmvc.xml:handler,拦截器,包扫描,视图解析器,文件下载的配置
Spring.xml样例:
<?xml version="1.0" encoding="UTF-8"?> <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" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 扫描 --> <context:component-scan base-package="com.thekingqj"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 管理数据源dataSource --> <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="username" value="${db.username}"></property> <property name="password" value="${db.password}"></property> <property name="url" value="${db.url}"></property> <property name="driverClassName" value="${db.driver}"></property> </bean> <!-- spring 整合jdbc 整合:使用spring管理jdbc jdbcTemplate :操作数据库的工具类 dbutils baseDao jdbcutil --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="datasource"></property> </bean> <!-- 配置事务管理器: 管理事务 --> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource"></property> </bean> <!-- 启用注解事务管理 --> <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/> </beans>SpringMVC.xml 样例:
<?xml version="1.0" encoding="UTF-8"?> <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.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.thekingqj" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:default-servlet-handler/><!-- 默认放行静态的资源,但是放行过多了,需要加上下面的一起使用 --> <mvc:annotation-driven /><!-- 配置注解驱动 --> <!--配置文件上传解析器,注意,id名称必须是这个,不然可能会出错 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="1024000"></property> </bean> <!-- 配置拦截器 --> <!-- <mvc:interceptors> <bean id="firstHandlerInterceptor" class=""></bean> <bean id="secondHandlerInterceptor" class=""></bean> </mvc:interceptors> --> <!-- 简单映射的异常解析器 --> <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.RuntimeException">error</prop> </props> </property> </bean> </beans>通过WEB.xml加载前端控制器,同时创建SpringMVC容器,通过ContextLoaderListener监听创建Spring容器到ServletContext中去。
web.xml中两个容器的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>spring07</display-name> <!-- 通过监听将SpringIOC容器放入到,ServletContext中 --> <!-- needed for ContextLoaderListener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 加载前端控制器,生成SpringMVC容器 --> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>