SpringMVC与Spring集成

以前用的最多的是Struts2与Spring的集成,而Spring2.*到Spring3.*jar包方面做了很大的改变。

在Srping2.*中属于Spring的jar包是:spring.jar。

但是在Spring3.*以后spring.jar包被拆分成了各个子jar如:

spring-aop.3.2.0.RELEASE.jar; spring-aspects.3.2.0.RELEASE.jar; spring-beans.3.2.0.RELEASE.jar; spring-context.3.2.0.RELEASE.jar;
spring-context-support.3.2.0.RELEASE.jar; spring-core.3.2.0.RELEASE.jar; spring-expression.3.2.0.RELEASE.jar; spring-instrument.3.2.0.RELEASE.jar; 
spring-instrument-tomcat.3.2.0.RELEASE.jar; spring-jms.3.2.0.RELEASE.jar; spring-orm.3.2.0.RELEASE.jar; spring-oxm.3.2.0.RELEASE.jar;
spring-struts.3.2.0.RELEASE.jar; spring-test.3.2.0.RELEASE.jar; spring-tx.3.2.0.RELEASE.jar; spring-web.3.2.0.RELEASE.jar; 等等

先来看看Struts2与Spring集成的配置文件:

web.xml配置:

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

Struts2是通过filter加载的,而Spring则是通过listener加载。

注:web.xml文件中执行的顺序如下:
1. <context-param>
2. <listener>
3. <filter>
4. <servlet>

applicationContext.xml配置:

<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:p="http://www.springframework.org/schema/p"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
		<!-- 注册一个UserDaoImpl实例名称为dao -->
	<bean id="dao" class="test.spring.dao.impl.UserDaoImpl"/>
		<!-- 注册一个UserServiceImpl实例名称为service -->
	<bean id="service" class="test.spring.service.impl.UserServiceImpl">
		<!-- 将UserDaoImpl实例dao注入给UserServiceImpl实例的dao属性 -->
		<property name="dao" ref="dao"/>
	</bean>
		<!-- 注册一个UserAction实例名称为userAction -->
	<bean id="userAction" class="test.spring.action.UserAction">
		<!-- 将UserServiceImpl实例service注入给UserAction实例的service属性 -->
		<property name="service" ref="service" />
	</bean>	
</beans>
struts.xml配置:

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<constant name="struts.objectFactory" value="spring"></constant><!-- 告诉struts类通过Spring IOC加载 -->
	<package name="firstApp"  extends="struts-default">
		<action name="helloworld" class="HelloAction" method="excute"><!-- HelloAction这里是Spring配置bean的id -->
			<result name="success" >success.jsp</result>
			<result name="input">input.jsp</result>
		</action>
	</package>
</struts>
以为就是Struts2的与Spring的集成配置了。

下面再来看看SpringMVC与Spring的集成:
SpringMVC集成Spring不需要添加新的jar。

web.xml配置:

<context-param>	<!-- 要修改listener的路径需要在context-param中配置如下 -->
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath*:config/applicationContext.xml</param-value>
</context-param>
	
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
	
<servlet>
	<servlet-name>springMVC</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:config/spring-servletAnnotation.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
	<servlet-name>springMVC</servlet-name>
	<url-pattern>/*</url-pattern>
</servlet-mapping>
applicationContext.xml配置:

<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="springManager" class="com.edifier.controller.annotation.SpringManager"></bean>
</beans>

spring-servletAnnotation.xml配置:

<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 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-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- 注解扫描包 -->
    <context:component-scan base-package="com.edifier.springmvc.controller"></context:component-scan>
 
	<!-- 开启注解 -->
	<mvc:annotation-driven/>

	<!-- 【配置视图解析器】 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property><!-- 视图的前缀,相当于配置视图的目录 -->
		<property name="suffix" value=".jsp"></property><!-- 视图的后缀,相当于配置视图的格式如:".jsp" -->
	</bean>
</beans>
以上就配置完了。

在Struts2的Action类中要用SpringIOC注入属性需要使用set方法:

	private SpringManager springManager;

	public void setSpringManager(SpringManager springManager) {
		this.springManager = springManager;
	}
但是在SpringMVC中要用SpringIOC来来注入属性就显得简单多了:

	@Resource(name = "springManager")
	private SpringManager springManager;
代码量比Struts2明显少很多。


再来说说SpringMVC与Spring的上下文关系:

	//Spring的上下文属于根上下文
	WebApplicationContext webAppCont1 = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
	//SpringMVC的上下文继承了Spring的上下文
	WebApplicationContext webAppCont2 = RequestContextUtils.getWebApplicationContext(request);
	
	//所以这里用webAppCont2一样也可以获取springManager成功,在监听器中会用到这种上下文获取bean的方法。
	SpringService springManager = (SpringService) webAppCont1.getBean("springManager");








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值