action 的实例化方式

框架版本
方式一:单独使用struts2通过 return clazz.newInstance(); //实例化 方式二:
1)配置方式一:由spring 代码中的代码片段: bean = autoWiringFactory.autowire(clazz, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
产生的action实例。 即:每次都new一下。 2)配置方式二:spring来生成action的实例。必须配置 scope="prototype" ,
否则spring创建的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将是单例的,线程不安全,会引发数据混乱。
所以建议大家使用方式一,而不是方式二。

猜你喜欢

转载自blog.csdn.net/qq_20610631/article/details/82426099