struts2.0.14何时创建的action实例?

框架版本

struts2.0.14  xwork2.0.7  spring2.0.8

 

在web应用启动的时候,所有的action都已经通过装载到classloader了。

 

1、单独使用struts2时action是如何实例化的?

com.opensymphony.xwork2.ObjectFactory 中的方法buildAction()

然后跟踪buildAction方法,不难找到action是如何实例化的

第一步:

    public Object buildAction(String actionName, String namespace, ActionConfig config, Map extraContext) throws Exception {
        return buildBean(config.getClassName(), extraContext);
    }

第二步:

    public Object buildBean(String className, Map extraContext) throws Exception {
        return buildBean(className, extraContext, true);
    }

第三步:

    public Object buildBean(String className, Map extraContext, boolean injectInternal) throws Exception {

        Class clazz = getClassInstance(className);
        Object obj = buildBean(clazz, extraContext);
        if (injectInternal) {
            injectInternalBeans(obj);
        }

        return obj;
    }

第四步:

    public Object buildBean(Class clazz, Map extraContext) throws Exception {
        return clazz.newInstance(); //实例化
    }

 

2、struts2和spring结合的时候,action是如何实例化的?

 

com.opensymphony.xwork2.spring.SpringObjectFactory

 

 

DefaultActionInvocation

 

protected void createAction(Map contextMap) {

 

ObjectFactory

    public Object buildAction(String actionName, String namespace, ActionConfig config, Map extraContext) throws Exception {

        return buildBean(config.getClassName(), extraContext);

}

 

 

springObjectFactory

   /**

     * @param clazz

     * @param extraContext

     * @throws Exception

     */

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

        Object bean;

        //先调用子类的buildBean然后调用父类的

        try {

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

        } catch (UnsatisfiedDependencyException e) {

        System.out.println("fall back 实例化"); //Fall back

            bean = super.buildBean(clazz, extraContext);

        }

 

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

    }

======然后是spring=的源代码 (一开始没有超这个方面想,浪费了很多的时间)===============

    public Object autowire(Class beanClass, int autowireMode, boolean dependencyCheck)

           throws BeansException {

 

       // Use non-singleton bean definition, to avoid registering bean as dependent bean.

       RootBeanDefinition bd = new RootBeanDefinition(beanClass, autowireMode, dependencyCheck);

       bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); //scope=”prototype”

       if (bd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR) { //通过构造方法来构造一个类的实例

           return autowireConstructor(beanClass.getName(), bd, null).getWrappedInstance(); //在这里通过构造方法创建了一个新的实例

       }

       else {

           Object bean = getInstantiationStrategy().instantiate(bd, null, this);

           populateBean(beanClass.getName(), bd, new BeanWrapperImpl(bean));

           return bean;

       }

    }

 

 

通过上面的分析,在spring和struts2结合的时候

采用方式一:

在struts2-你的应用.xml中配置action

  <action name="detail" class="com.work.code.codegroup.CodeGroupAction"
   method="load">
   <result>/code/codegroup/detail.jsp</result>
  </action>

 

spring默认的是default-autowire="byName"  ,spring的配置文件中只配置你的service层和dao层即可,不需要再配置action了

 

方式二:

 

  <action name="detail" class="CodeGroupAction"
   method="load">
   <result>/code/codegroup/detail.jsp</result>
  </action>

 

 

 <bean id="CodeGroupAction" class="com.work.code.codegroup.CodeGroupAction" scope="prototype" />

 <bean id="codeGroupDao" class="com.work.code.codegroup.CodeGroupDaoJdbcImpl">
  <property name="dataSource" ref="dataSource" />
 </bean>
 <bean id="codeGroupServiceDao"
  class="com.work.code.codegroup.CodeGroupServiceDaoImpl">
  <property name="codeGroupDao" ref="codeGroupDao" />
  <property name="codeClassDao" ref="codeClassDao" />
 </bean>

 

注意:必须配置 scope="prototype" ,否则spring创建的action将是单例的,线程不安全,会引发数据混乱。

所以建议大家使用方式一,而不是方式二。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值