Spring与SpringMVC整合

一、修改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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值