ObjectFactory,是xwork中很重要的一个类,是产生action的地方。单独使用xwork的时候,action都是这个类创建的。
后来Struts2包装了ObjectFactory,自成了一个StrutsObjectFactory,他是继承自ObjectFactory的一个类,因此Struts2默认产生action的类就不使用xwork的ObjectFactory了,而是使用StrutsObjectFactory。
Struts2的插件struts2-spring-plugin-2.1.8.1.jar,又做了一个对象工厂,StrutsSpringObjectFactory,它的父类是xwork的SpringObjectFactory,最原始的父类也是ObjectFactory,我想xwork提供SpringObjectFactory的目的就是让实现这继承这个类,实现Spring工厂。
于是Struts2中也就是说有3个对象工厂,
org.apache.struts2.spring.StrutsSpringObjectFactory(Struts2的Spring插件提供)
org.apache.struts2.impl.StrutsObjectFactory(struts2自带)
com.opensymphony.xwork2.ObjectFactory(xwork自带)。
他们都可以用于产生action。
在xwork中默认使用ObjectFactory,在Struts2中默认使用StrutsObjectFactory。使用Struts2的Spring插件的时候默认使用StrutsSpringObjectFactory,这些都是自动的,其中前两个都是Struts2的内容。都是由
<constant name="struts.objectFactory" value="spring" />
来控制,这里的value,就是
<bean name="spring" type="com.opensymphony.xwork2.ObjectFactory" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
在这bean里面定义的是哪个工厂,Struts2就用哪个工厂。
但是不管是哪个工厂最终这些都是有xwork控制完成的。
我们可以查看xwork的DefaultActionInvocation类的createAction(Map<String, Object> contextMap)方法。这个方法就是工厂对象产生action的方法。我们可以看到这个类中的定义的对象工厂是
protected ObjectFactory objectFactory;
然后没有实例化,而是通过xwork的注入机制注入
@Inject
public void setObjectFactory(ObjectFactory fac) {
this.objectFactory = fac;
}
这个地方用的是最原始的ObjectFactory工厂,但是通过我们的配置为什么能变成StrutsSpringObjectFactory或StrutsObjectFactory呢?
这个还没有看完,但我想是多态造成的,因为ObjectFactory是StrutsSpringObjectFactory和StrutsObjectFactory的父类,可以用任意的子类实例化父类。我想是通过解读
<constant name="struts.objectFactory" value="spring" />
原文为 <constant name="struts.objectFactory" value="xia" />, 余认为不妥。
这个配置,来确定注入 ObjectFactory的那个子类。
我们可以在DefaultActionInvocation的createAction方法打印一下objectFactory
会发现,使用不同的对象工厂,那么这里会打印相应的工厂。绝对不只是ObjectFactory这个父类。
说明: 此文纯粹是从网络摘抄而来, 也根据此阅读了源码, 学习良多, 印象深刻, 特此分享之。
分享到:
相关推荐
Struts2与Spring的集成要用到Spring插件包struts2-spring-plugin-x-x-x.jar,这个包是同Struts2一起发布的。Spring插件是通过覆盖(override)Struts2的ObjectFactory来增强核心框架对象的创建。当创建一个对象的...
Spring注解配置中间层供Struts2使用 1.spring通过注解方式生成中间层 2.不使用Struts2的常量struts.objectFactory也可识别中间层
该属性指定是否允许在Struts 2标签中使用表达式语法,因为通常都需要在标签中使用表达式语法,故此属性应该设置为true,该属性的默认值是true. struts.devMode 该属性设置Struts 2应用是否使用开发模式.如果设置该属性...
struts.objectFactory = spring struts.i18n.reload=true #struts.locale=zh_CN struts.i18n.encoding=GBK ) web.xml(仅写出中文处理部分,spring的配置不用改变) <filter-name>encodingFilter <filter-class>...
是否struts过滤器中提供的静态内容应该被浏览器缓存在头部属性中 struts.serve.static Whether the Struts filter should serve static content or not 是否struts过滤器应该提供静态内容 struts.tag....
”这个插件重写了struts的对象工厂,当创建一个action类时,它会根据struts的配置文件的class属性的值与 spring配置文件中的id属性的值相匹配。如果没有与之相匹配,将会像没有使用这个插件前一样创建,然后由spring...
该属性指定是否允许在Struts 2标签中使用表达式语法,因为通常都需要在标签中使用表达式语法,故此属性应该设置为true,该属性的默认值是true。 struts.devMode 该属性设置Struts 2应用是否使用开发模式。如果...
struts 2.3.4.1 最新英文版...com.opensymphony.xwork2.spring Spring ObjectFactory classes. com.opensymphony.xwork2.spring.interceptor Spring specific interceptor classes. com.opensymphony.xwork2.test ...
Struts与Spring整合:在Struts中的配置重点<constant name="struts.objectFactory" value="spring
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" /> 修改struts默认的拦截: <filter-name>struts2 *.action *.jsp 2、添加hibernate功能 3、添加...
com.opensymphony.xwork2 ...com.opensymphony.xwork2.spring Spring ObjectFactory classes. com.opensymphony.xwork2.spring.interceptor Spring specific interceptor classes. com.opensymphony.xwork2.test ...
下面是整合的的步骤 spring整合hibernate 加入jar包 加入spring和aop所需必须包 加入hibernate的必须包 ...在struts.xml中配置对象创建工具为spring <constant name="struts.objectFactory" value="spring" />
<constant name="struts.objectFactory" value="spring" /> <!-- 更改struts2请求Action的后缀名,默认为action。若想去掉后缀,设为","即可 --> <constant name="struts.action.extension" value=","/> <!-...
struts.xml不需要改变,因为引入struts-spring-plugin.jar后,Action创建会交给插件的ObjectFactory. web.xml需要定义ContextLoaderListener,实例化容器配置 5.将事务管理交个Spring,采用AOP方式,删除原有Struts事务...
│ spring.xml │ struts.xml │ ├─28个java常用的工具类 │ │ Base64.java │ │ Base64DecodingException.java │ │ CConst.java │ │ CharTools.java │ │ ConfigHelper.java │ │ Counter.java │ │ C...