`
胡小米
  • 浏览: 75978 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Struts2与Spring集成中的自动装配策略

阅读更多
1.    与自动装配有关的配置

【org.apache.struts2.StrutsConstants类】

Ø // Spring应该如何装配。有效值:’name’, ’type’, ’auto’ 和’construtctor’。

STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE

Ø // 由STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE选择的自动装配策略是否总是受重视的。默认是false,遗留行为即试图根据情况决定最好的策略。设置为true表示使用STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE设置的自动装配策略。

STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE_ALWAYS_RESPECT

Ø 3(// Spring是否使用它的类缓存

STRUTS_OBJECTFACTORY_SPRING_USE_CLASS_CACHE

2.    自动装配原理

首先根据在Struts.xml中定义Action时指定的class属性值获取Action实例

即appContext.getBean(beanName)

n 如果获取到,则直接返回。此时所使用的自动装配策略是applicationContext.xml中设置的装配策略。

applicationContext.xml中beans的默认自动装配策略是no,所以如果没有设置<beansdefault-autowire="autodetect">或者bean的autowire="byName",则Action中的属性比如personManager不会进行自动装配。

n 如果获取不到,则调用buildBean(beanClazz, extraContext)。

u 如果struts.objectFactory.spring.autoWire.alwaysRespect为true,此时会根据Struts定义的自动装配策略(struts.objectFactory.spring.autoWire)进行自动装配。

u 如果struts.objectFactory.spring.autoWire.alwaysRespect为false,则按constructor方式进行自动装配。

参考SpringObjectFactory.java源代码

    @Override

    public Object buildBean(String beanName, Map<String, Object> extraContext, boolean injectInternal) throws Exception {

        Object o = null;

        try {

            o = appContext.getBean(beanName);

        } catch (NoSuchBeanDefinitionException e) {

            Class beanClazz = getClassInstance(beanName);

            o = buildBean(beanClazz, extraContext);//使用Struts定义的自动装配策略

        }

        if (injectInternal) {

            injectInternalBeans(o);

        }

        return o;

    }

    public Object buildBean(Class clazz, Map<String, Object> extraContext) throws Exception {

        Object bean;

        try {

            // Decide to follow autowire strategy or use the legacy approach which mixes injection strategies

            if (alwaysRespectAutowireStrategy) {

                // Leave the creation up to Spring

                bean = autoWiringFactory.createBean(clazz, autowireStrategy, false);

                injectApplicationContext(bean);

                return injectInternalBeans(bean);

            } else {

                bean = autoWiringFactory.autowire(clazz, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);

                bean = autoWiringFactory.applyBeanPostProcessorsBeforeInitialization(bean, bean.getClass().getName());

                // We don't need to call the init-method since one won't be registered.

                bean = autoWiringFactory.applyBeanPostProcessorsAfterInitialization(bean, bean.getClass().getName());

                return autoWireBean(bean, autoWiringFactory);

            }

        } catch (UnsatisfiedDependencyException e) {

            if (LOG.isErrorEnabled())

                LOG.error("Error building bean", e);

            // Fall back

            return autoWireBean(super.buildBean(clazz, extraContext), autoWiringFactory);

        }

    }

所有有两种配置方式

第一种:参见1集成步骤中的配置

applicationContext.xml(配置beans的自动装配策略)

struts.xml中action的class属性值与application-context.xml的bean的id相同。

第二种:

applicationContext.xml(不配置beans的自动装配策略)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="login" class="com.jeff.action.LoginAction" scope="prototype">

</bean>
<bean id="personManager" class="com.jeff.service.PersonManager" scope="prototype">

</bean>
...
</beans>

struts.xml(struts.xml中action的class属性值与application-context.xml的bean的id不同,而是设置为Action的类名)

<package name="first" extends="struts-default" >

<action name="login1" class="com.jeff.action.LoginAction1">

<result>/loginInfo.jsp</result>

</action>

</package>

第二种配置方式,struts.objectFactory.spring.autoWire才有可能起作用。
分享到:
评论

相关推荐

    整合struts2和spring源代码(可以直接在tomcat中运行)

    Struts2与Spring的集成要用到Spring插件包struts2-spring-plugin-x-x-x.jar,这个包是同Struts2一起发布的。Spring插件是通过覆盖(override)Struts2的ObjectFactory来增强核心框架对象的创建。当创建一个对象的...

    spring2.5学习PPT 传智博客

    28.Struts与Spring集成方案2(Spring集成Struts) 29.为Spring集成的Hibernate配置二级缓存 30.Spring提供的CharacterEncoding和OpenSessionInView功能 31.使用Spring集成JPA 32.Struts+Spring+JPA集成 33.使用...

    Android+ssh项目综合实践

    在我们集成struts2+spring+hibernate,也就是所谓的S2SH,不可避免的要引入struts2-spring- plugin.jar插件。当引入这个插件后,原先所struts创建的action类,交给了spring创建。在struts2-spring- plugin.jar中有一...

    Spring in Action(第二版 中文高清版).part2

    16.2 协同使用Spring和WebWork 2/Struts 2 16.3 集成Spring和Tapestry 16.3.1 集成Spring和Tapestry 3 16.3.2 集成Spring和Tapestry 4 16.4 协同使用Spring和JSF 16.4.1 解析JSF管理的属性 16.4.2 解析Spring...

    Spring-Reference_zh_CN(Spring中文参考手册)

    2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的...

    Spring in Action(第二版 中文高清版).part1

    16.2 协同使用Spring和WebWork 2/Struts 2 16.3 集成Spring和Tapestry 16.3.1 集成Spring和Tapestry 3 16.3.2 集成Spring和Tapestry 4 16.4 协同使用Spring和JSF 16.4.1 解析JSF管理的属性 16.4.2 解析Spring...

    Spring中文帮助文档

    3.3.5. 自动装配(autowire)协作者 3.3.6. 依赖检查 3.3.7. 方法注入 3.4. Bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. Singleton beans和prototype-bean的依赖 3.4.4. 其他作用域 ...

    Spring in Action(第2版)中文版

    16.2协同使用spring和webwork2/struts2 16.3集成spring和tapestry 16.3.1集成spring和tapestry3 16.3.2集成spring和tapestry4 16.4协同使用spring和jsf 16.4.1解析jsf管理的属性 16.4.2解析springbean 16.4.3...

    Spring攻略(第二版 中文高清版).part2

    6.3 将Spring与Struts 1.x集成 220 6.3.1 问题 220 6.3.2 解决方案 220 6.3.3 工作原理 220 6.4 将Spring与JSF集成 226 6.4.1 问题 226 6.4.2 解决方案 226 6.4.3 工作原理 227 6.5 将Spring与DWR...

    spring in action英文版

     2.3.1 处理自动装配中的不确定性  2.3.2 混合使用自动和手动装配  2.3.3 缺省自动装配  2.3.4 何时采用自动装配  2.4 使用Spring的特殊Bean  2.4.1 对Bean进行后处理  2.4.2 对Bean工厂进行后...

    Spring攻略(第二版 中文高清版).part1

    6.3 将Spring与Struts 1.x集成 220 6.3.1 问题 220 6.3.2 解决方案 220 6.3.3 工作原理 220 6.4 将Spring与JSF集成 226 6.4.1 问题 226 6.4.2 解决方案 226 6.4.3 工作原理 227 6.5 将Spring与DWR...

    低清版 大型门户网站是这样炼成的.pdf

    2.3.4 struts 2中集成displaytag 83 2.4 struts 2国际化实现 85 2.4.1 web应用的中文本地化 85 2.4.2 struts 2应用的国际化 87 2.4.3 struts 2国际化语言的动态切换 89 2.5 struts 2的校验框架 90 2.5.1 在...

    Spring 2.0 开发参考手册

    3.3.6. 自动装配(autowire)协作者 3.3.7. 依赖检查 3.3.8. 方法注入 3.4. bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. 其他作用域 3.4.4. 自定义作用域 3.5. 定制bean特性 3.5.1...

    Spring API

    3.3.5. 自动装配(autowire)协作者 3.3.6. 依赖检查 3.3.7. 方法注入 3.4. Bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. Singleton beans和prototype-bean的依赖 3.4.4. 其他作用域 ...

    spring chm文档

    3.3.6. 自动装配(autowire)协作者 3.3.7. 依赖检查 3.3.8. 方法注入 3.4. bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. 其他作用域 3.4.4. 自定义作用域 3.5. 定制bean特性 3.5.1...

    Java Web程序设计教程

    第14章spring与struts2、hibernate框架的整合基础 277 14.1spring与struts2的整合方式 277 14.1.1struts2应用的扩展方式 277 14.1.2spring插件的应用 278 14.2spring和hibernate的整合 279 14.2.1spring对...

    尚学堂spring课程培训代码

    讲述的内容:aop, autowire(自动装配),动态代理、静态代理、注解、hibernate集成、struts集成等,欢迎关注我的其他资源!

    JAVA程序开发大全---上半部分

    以及基于这些技术的商业化应用程序的开发技巧,在讲解过程中以目前最为流行的开发工具MyEclipse为载体,全面系统地介绍了如何在MyEclipse中开发基于Struts、Hibernate、Spring等主流框架的各种Java应用程序。...

    火炬博客系统7

    可以很好的支持AOP(面向切面编程)的开发模式,Spring能有效地组织中间层对象,通过Bean容器为业务对象、DAO对象和资源对象提供了IOC类型的装配能力,将Struts和Hibernate集成起来,使用Spring构建的应用程序易于...

Global site tag (gtag.js) - Google Analytics