Spring整合Strut2小结
2015-06-28 10:12
441 查看
自己动手试下整合spring和Strut2的最简单的一个小实验,使用的版本是struts-2.3.24和spring-framework-4.1.4.RELEASE-dist。整合参考的struts2文档介绍url:http://struts.apache.org/docs/spring-plugin.html由于没有经验出现了问题和收获,小结如下:1.只搭struts2能正常运行,一加上spring就出问题了,程序运行不起来,tomcat报了各自错误原因是需要的jar包没有全部导入,之前学习看的视频是spring2.5.6的,它的spring.jar包里面包含了各自需要的jar包集合,而spring4.1.4是分开在libs目录下,最后可以正常整合需要的最小包如下图(正常运行不包括spring的其他事务通知,aop控制),总结一句:不同版本的整合可能需要不同的jar包,为了减少折腾应该从框架相应文档目录或者原来项目或者平时整合资料中寻找现成需要的jar包.例如当时struts2的包就是从struts-2.3.24-all\struts-2.3.24\apps\struts2-blank的libcopy出来用的,运行时发现缺少commons-loging,添加上后正常了2.搭struts2框架时候经常出现找不到mapping action或者404错误解决思路,首先看路径是否写对,然后再看tomac时候正常启动该web app,如果发生了异常,从异常着手。3 小结spring整合struts2的粗浅认识机制spring负责了strut2 action对象的创建,当stucts的拦截器收到action请求时,首先strut2会去查看struts.xml 该action请求对应的class属性是否在spring容器里有对应的id属性,如果有则用该对象去处理action请求,不会再创建新的对象。如果没有在spring容器中找到对应的id,spring则会自己创建该对象,并完成的系列的依赖注入。2个做实验的类 FirstAction 和 Apple,并把容器中的Apple类实例注入到FirstAction实例对象中中:
package spring.scope; import javax.annotation.Resource; public class FirstAction { private Apple apple; private String id; public FirstAction() { System.out.println("FirstAction 被创建了"); } public String fun() { System.out.print("apple is " + apple); return "success"; } public Apple getApple() { return apple; } @Resource(name="apple") public void setApple(Apple apple) { this.apple = apple; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
package spring.scope; import org.springframework.stereotype.Component; public class Apple { public Apple() { System.out.println("Apple类被创建了。。"); } }struts.xml 和 spring.xml注意:strut2.xml的firstaction的class属性和sprig.xml 的id属性
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <span style="white-space:pre"> </span> <constant name="struts.objectFactory" value="spring" /> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span><package name="test" extends="struts-default"> <span style="white-space:pre"> </span><action name="firstaction" class=<span style="color:#ff0000;">"firstaction</span>" method="fun"> <span style="white-space:pre"> </span><result name="success">/success.html</result> <span style="white-space:pre"> </span></action> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span></package> </struts>
注意:为了现实效果把id为apple的bean scope属性设置为prototype
<?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/><context:component-scan base-package="spring"/><bean id="apple" class="spring.scope.Apple" scope="<strong><span style="color:#ff0000;">prototype</span></strong>"/><bean id="<strong><span style="color:#ff0000;">firstaction</span></strong>" class="spring.scope.FirstAction"><property name="id" value="2013"/></bean></beans>运行结果如下:当tomcat启动的时候并加载该web项目,这2个对象被spring 容器 创建!!随后我尝试多次请求改action,只输出fun方法的内容,没有创建新的firstaction(Action是多例的)从图中还能看出apple对象是同一个对象,证明了这句话”首先strut2会去查看struts.xml 该action请求对应的class属性是否在spring容器里有对应的id属性,如果有则用该对象去处理action请求,不会再创建新的对象“,如图如果把 struts.xml文件的 action name="firstaction" class="firstaction" method="fun" 改成 actionname="firstaction" class="spring.scope.FirstAction" method="fun",运行结果如下,当tomcat启动的时候并加载该web项目,这2个对象被spring 容器 创建!!随后我尝试多次请求该action,发现每次请求都产生了新的Apple对象和FirstAction对象,并且可以看到Apple每次都不是同一个对象,判断出这个Action对象是由spring创建并完成了依赖注入的并非struts创建,证明了这句话”。如果没有在spring容器中找到对应的id,spring则会自己创建改对象,并完成的系列的依赖注入“
相关文章推荐
- 2015062805 - EffactiveJava笔记 - 第41条 慎用重载(1)
- 使用eclipse编译和管理live555源码
- jsp显示java后台的异常
- eclipse运行jsp程序错误---SEVERE: StandardServer.await: create[localhost:8005]
- Java内部类总结
- Eclipse配色方案插件
- JavaWeb工程中web.xml基本配置
- Java中的匿名内部类:由setOnClickListener说起
- Java基础---设计模式
- Java环境变量的配置问题与部分基础知识
- 深入java--与MySQL连接时的时间类问题以及Calendar的用法
- Eclipse配置maven环境笔记
- 2015062802 - EffactiveJava笔记 - 第40条 谨慎设计方法签名
- java实现文件夹(包括其中的子文件夹、子文件)的复制——递归
- 在Struts等框架中获取Spring容器的方式
- java.io.Writer
- Java基础:动态代理在RPC框架中应用
- Java基础:静态代理和动态代理
- Javascirpt 作用域
- java当前日期增加天数