一、修改Spring容器的创建方式
之前在使用Spring的时候我们使用的是ApplicationContext接口来获取spring容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
改进:改为像SpringMVC一样在配置文件中创建容器
- 配置web.xml
<!-- spring -->
<!-- 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 -->
<!-- 通过监听器来创建Spring容器 -->
<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>
二、资源管理的问题
之前我们是将所有的注解扫描都加载到Spring或SpringMVC中,现在吧两个整合到一起,我们需要进行一下分工
- spring.xml: dao,service,数据源,声明式事务,包扫描…
- springmvc.xml: handler(controller),拦截器,包扫描,视图解析器
Spring
<!-- 包扫描 -->
<context:component-scan base-package="com.baidu.ss">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 引入外部文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 管理数据源 -->
<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>
<!-- 配置事务管理器: 管理事务 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用注解事务管理 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
SpringMVC
<!-- 包扫描 -->
<context:component-scan base-package="com.baidu.ss" annotation-config="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 /><!-- 使用默认的servlet处理静态资源访问 -->
<mvc:annotation-driven /><!-- 启用注解驱动 -->
<!-- 文件上传解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="1024000"></property>
<property name="maxInMemorySize" value="1024000"></property>
</bean>
<!-- 拦截器 -->
<mvc:interceptors>
<!-- 声明自定义拦截器 -->
<bean id="firstHandlerInterceptor"
class="com.baidu.springmvc.interceptors.FirstHandlerInterceptor"></bean>
<bean id="secondHandlerInterceptor"
class="com.baidu.springmvc.interceptors.SecondHandlerInterceptor"></bean>
</mvc:interceptors>