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

自动装配即bean之间的依赖关系无需手动配置。

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)

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

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

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

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

如果struts.objectFactory.spring.autoWire.alwaysRespectfalse,则按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) throwsException {

        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_CONSTRUCTORfalse);

                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.xmlactionclass属性值与application-context.xmlbeanid相同。

第二种:

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.xmlstruts.xmlactionclass属性值与application-context.xmlbeanid不同,而是设置为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才有可能起作用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值