父子容器
典型的父子容器就是spring和Springmvc同时使用的时候。分别ContextLoaderListener 创建的 Spring容器是父容器,DispatcherServlet 创建的Springmvc容器是子容器。
子容器可以使用getBean()获得父容器里的bean对象,父容器不能获得子容器的bean对象。
那么为什么我们在项目中还要联合用到Spring容器和SpringMVC容器?
答案是: Spring的扩展性,如果要是项目需要加入Struts等可以整合进来,便于扩展框架。如果要是为了快,为了方便开发,完全可以用SpringMVC框架。
容器分离的影响:
- 一般在父容器中会加载service,dao之类的东西,不加载controller
- 在springmvc容器中只加载controller
Spring父容器负责所有其他非@Controller注解的Bean的注册,而SpringMVC只负责@Controller注解的Bean的注册。
1、在web.xml中配置
先去启动applicationContext.xml把它作为父容器,然后把spring-mvc.xml里面所有创建出来的对象作为子容器内容放到父容器中
启动父容器
<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>
启动子容器
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2、spring 父容器 applicationContext.xml中配置
<context:component-scan base-package="zhibo.service"></context:component-scan>
<context:component-scan base-package="zhibo.dao"></context:component-scan>
<!--DruidDataSource数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="root"></property>
<property name="password" value="235689"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db_zbmanager?serverTimezone=UTC"></property>
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lanou.zhibo.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 注解式开启事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启动事务的注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
3、springmvc子容器中配置
<context:component-scan base-package="zhibo.controller"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 静态资源 作用是:不让静态资源过前端控制器-->
<mvc:resources mapping="/static/**" location="/static/"></mvc:resources>
<!-- 拦截器-->
<mvc:interceptors>
<!-- 将拦截器配置在mvc:interceptor里它就只关心这里面映射的链接-->
<!-- 配置在mvc:interceptor外,它关心所有的映射的链接-->
<mvc:interceptor>
<mvc:mapping path="/user/**"/>
<ref bean="myInterceptor"></ref>
</mvc:interceptor>
</mvc:interceptors>
<bean id="myInterceptor" class="controller.MyInterceptor"></bean>
<<!-- 配置文件上传-->
<bean id="MultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- defaultEncoding 编码格式
maxInMemorySize在内存的最大值,影响的是文件的上传速度和完整度
maxUploadSize上传文件的大小
maxUploadSizePerFile 用于多上传文件,设置每个上传文件的大小-->
<property name="defaultEncoding" value="utf8"></property>
</bean>