英文内容来源:http://struts.apache.org/2.0.6/docs/spring-plugin.html
http://blog.csdn.net/jameslee666/archive/2007/03/28/1543802.aspx
Spring is a lightweight container, providing centralized, automated configuration and wiring of your application objects, using a technique called "Dependency Injection" Spring是一个轻量级容器,它用“依赖注入”的技术提供集成和自动的应用对象配置。 |
The Spring Plugin works by overriding the Struts ObjectFactory to enhance the creation of core framework objects. When an object is to be created, it uses the class attribute in the Struts configuration to correspond to the id attribute in the Spring configuration. If not found, the class will try to be created as usual, then be
autowired by Spring. In the case of Actions, Spring 2's bean scope feature can be used to scope an Action instance to the session, application, or a custom scope, providing advanced customization above the default per-request scoping.
Spring 插件在Struts ObjectFactory上面运行,能增强核心框架对象的创建能力。当一个对象被创建时,这个对象会使用Struts 配置的类属性来装配Spring配置里的id属性。如果找不到类,Spring会设法按常规装配地创建一个。在Action里,Spring 2 的bean scope feature(bean的范围特性)用来控制Action实例在session、application的使用范围,或控制Action实例的自定义的使用范围,以便在默认的请求范围基础上提供高级的定制化服务。
Spring Actions are Optional! Spring Actions只是可选项 Remember: registering Actions with Spring is not required. The Spring alternative is there if you need it, but the framework will automatically create Actions objects from the action mappings. But, if you want to use Spring to inject your Actions, the option is there. |
Features
特性
- Allow Actions, Interceptors, and Results to be created by Spring
- Struts-created objects can be autowired by Spring after creation
- Provides two interceptors that autowire actions, if not using the Spring
- 允许Spring创建Action、拦截器和记录集
- Struts创建对象之后自动跟Spring装配
- 如果不用Spring的ObjectFactory 对象工厂,框架将提供两个拦截器来装配Action。
Usage
用法
To enable Spring integration, simply include struts2-spring-plugin-x-x-x.jar in your application.
为了能和Spring集成,你需要把struts2-spring-plugin-x-x-x.jar放在你的应用里。
If you are using more than one object factory, (for example, by including both the Spring and Plexus plugins in your application,) you will need to set the struts.objectFactory property in struts.properties or in one of several XML files via Constant Configuration:
如果你使用一个以上的对象工厂(比如在你的应用中即用Spring又用Plexus),那么你需要在struts.properties 或Constant Configuration的XML文件里设置struts.objectFactory 属性:
struts.objectFactory = spring
<struts> <constant name="struts.objectFactory" value="spring" /> ... </struts>
Autowiring
自动装配
The framework enables "autowiring" by default. (Autowiring means to look for objects defined in Spring with the same name as your object property). To change the wiring mode, modify the spring.autowire property.
框架里默认是自动装配的(Autowiring自动装配是指自动寻找Spring用跟你的对象属性名字一样定义的对象 )。要改变装配模式,那么你可以修改spring.autowire 的属性
装配模式
struts.objectFactory.spring.autoWire = type
The autowire property can be set to several options.
自动装配属性有几个选项可设置。
Auto-wire by matching the name of the bean in Spring with the name of the property in your action. This is the default 自动装配会根据Spring里的bean的名字和你的Action里的属性名字来装配。这个选项是默认值。 |
Auto-wire by looking for a bean registered with Spring of the same type as the property in your action. This requires you to have only one bean of this type registered with Spring 自动装配会自动寻找Spring里注册的跟你的action里类型属性一样的bean。这就要求在Spring里只能注册一个唯一的类型。 |
Spring will attempt to auto-detect the best method for auto-wiring your action Spring会尝试自动发觉装配你的action的最佳方法。 |
Spring will auto-wire the parameters of the bean's constructor Spring会自动装配bean的构造器参数。 |
By default, the framework will at least try to use Spring to create all its objects. If the object cannot be created by Spring, then the framework will create the object itself.
默认情况下,框架会设法用Spring去创建Spring的全部对象。如果Spring不能创建对象,那框架会创建。
Enabling Spring integration for other application objects is a two-step process.
两个步骤可使Spring能跟其他应用对象集成。
- Configure the Spring listener
设置Spring监听器
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
- Register your objects via the Spring configuration
- 通过Spring的配置来注册你的对象
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans default-autowire="autodetect"> <bean id="personManager" class="com.acme.PersonManager"/> ...</beans>
More applicationContext configuration files needed? 还需要其他applicationContext 配置文件吗? Since the Spring integration uses a standard Listener, it can be configured to support configuration files other than applicationContext.xml. Adding the following to your web.xml will cause Spring's ApplicationContext to be inititalized from all files matching the given pattern: <!-- Context Configuration locations for Spring XML files --><context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value></context-param> See the Spring documentation for a full description of this parameter. |
Initializing Actions from Spring
从Spring初始化Action
Normally, in struts.xml you specify the class for each Action. When using the default SpringObjectFactory, the framework will ask Spring to create the Action and wire up dependencies as specified by the default auto-wire behavior.
通常情况下,在struts.xml里,你为每一个Action指定类。当使用默认的SpringObjectFactory时,框架会要求Spring去创建Action并根据默认的自动装配行为去指定装配依赖。
We strongly recommend that you find declarative ways of letting Spring know what to provide for your actions. This includes making your beans able to be |
However, sometimes you might want the bean to be completely managed by Spring. This is useful, for example, if you wish to apply more complex AOP or Spring-enabled technologies, such as Acegi, to your beans. To do this, all you have to do is configure the bean in your Spring applicationContext.xml and then change the class attribute from your Action in the struts.xml to use the bean name defined in Spring instead of the class name.
然而,有时候你可以让bean完全被Spring控制。这个是有用的,举个例子,如果你希望申请更多复杂的AOP或Spring的有用技术,如Acegi,到你的bean里。为此,你只要在你的Spring applicationContext.xml设置你的bean,然而改变在struts.xml 里的Action的类属性,用Spring里定义的bean名字来代替类的名字。
Your struts.xml file would then have the Action class attributes changed.
你的 struts.xml 里的Action类的属性会被修改。
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <include file="struts-default.xml"/> <package name="default" extends="struts-default"> <action name="foo" class="com.acme.Foo"> <result>foo.ftl</result> </action> </package> <package name="secure" namespace="/secure" extends="default"> <action name="bar" class="bar"> <result>bar.ftl</result> </action> </package></struts>
Where you have a Spring bean defined in your applicationContext.xml named "bar". Note that the com.acme.Foo Action did not need to be changed, because it can be autowired.
你有一个在你的applicationContext.xml以"bar"命名的Spring bean。注意:com.acme.Foo 不需要做修改,因为它会被自动装配。
A typical spring configuration for bar could look as following.
一个典型的bar的spring配置像如下代码:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans default-autowire="autodetect"> <bean id="bar" class="com.my.BarClass" singleton="false"/> ...</beans>
To use session-scoped components with Spring and Struts, see the Spring Session Components Workarounds analysis.
Settings
The following settings can be customized. See the developer guide.
struts.objectFactory.spring.autoWire | The autowire strategy | name | name,type,auto, or constructor |
struts.objectFactory.spring.useClassCache | Whether to have Spring use its class cache or not | true | true or false |
Installation
安装
This plugin can be installed by copying the plugin jar into your application's /WEB-INF/lib directory. No other files need to be copied or created.
插件通过拷贝插件jar到你的应用/WEB-INF/lib目录下来完成安装。不需要拷贝或复制其他文件。
相关推荐
Struts2+Spring+Hibernate集成开发环境的配置小例子。
2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。 4.在服务端分页查询功能,优点:实时性:跳页才查询。数据量小:只加载当前页的记录...
SSI+Mina2(Struts2+Spring4+Mybatis3+Mina2)集成,都是最新的包, 发布就可运行,配置文件都写了详细的注释。
1.使用struts2+spring3+hibernate3整合实现用户CRUD功能。 2.使用多种配置方式 A.将hibernate从外部导入,使用MySQL作为数据库。 B.将hibernate实体映射集成到spring配置文件中。 3.使用传统的配置文件集成...
Struts2+Spring4+MyBatis3 最新包集成发布就可以运行,配置文件都写了很详细的注释。
2、 Spring配置提供了多种可选整合方案:详见applicationContext.xml的配置 (1) 提供支持三种数据源方案:C3P0、DBCP、JNDI,三选一,请注释或删除其他方案 (2) 提取四种Hibernate整合方案:四选一,请注释或...
struts1+spring 一套完整的框架 配置好数据库就可以直接使用
struts2+spring3+mybatis3最基本的集成框架,如果需要用到这三个框架来开发项目的话,可以直接基于这个demo往下扩充开发。我也是项目中需要,也是为了以后用到类似的框架,可以直接拿来就用,比较方便。本人码出来的...
使用struts2+spring+cas实现的单点登录功能,里面包括cas-server3.5.2项目一个,cas-client3.2.1 web项目两个,数据库脚本,请按照里面的说明文档进行部署,希望你们也能配置成功。
Struts2.2.1+Spring3.1.0.M2+Hibernate3.5.1整合配置源码,在源码中简单地实现了查询、新增、删除操作。
本压缩文件中包含在struts2,hibernate,spring集成的时候 所需要用到的 三者的配置,又具体dao,service层以及action的配置,是学习S2SH集成好助手,能很好帮助你完成相关配置
本版本全面更新了jar包,全部使用了当前最新版本的jar包,struct2.1.8 spring3 hibernate3.5,全面使用注解取代xm的l配置。 另外增加了一个ant构建脚本,支持使用hudson完成每日构建,持续集成,自动测试,代码规范...
Jboss7上+maven搭建struts2+spring3+hibernate3完整能运行工程源码,在tomcat7上运行也成功测试了,注意:我工程文件是用myeclipse2013开发的,你需要导入自己ide的时候,会有兼容性问题:最好阁下是自己新建一个maven...
使用了框架是Struts1+hibernate+spring 集成了JQuery的FlexiGrid表格插件以及 jsmodal弹出框插件 一个增删改列表 数据库使用的是MYSQL 内有建表语句 数据库配置请自行配置本机数据库
本人自己动手集成的struts+spring+hibernate 网站后台管理系统,希望对刚学习struts的朋友带来一定的帮助,这是我上传此文件的最大愿望,里面有三个框架所需要的jar包和所有的配置文件,只要下载即可运行,不需要...
最新SSH集成+Extjs5+struts2.3.20+hibernate4.3.8+spring4.1.4 包括动态菜单配置功能
如果是入门学习,或者想看看struts+spring+ibatis怎么集成在一起,可以下载看看。已经通过测试。jar包全,需要本地装mysql,或者修改jdbc配置文件
Struts1.3+Spring3.0+Hibernate3.3+DWR2.X框架集成文档v1.4.pdf 包括事务处理配置,个人整理,仅供学习,请务拍砖。呵呵~~
SSH+Redis整合
数据库:sqlserver CreateHtmlTable中的生成路径和SendService中的上传路径改成自己的