Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-3147

com.opensymphony.xwork2.spring.SpringObjectFactory does'nt create bean when alwaysRespectAutowireStrategy=false

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Not A Problem
    • 2.1.6
    • 2.1.8
    • Plugin - Spring
    • None
    • Struts-2.1.6
      TC 6.0.14
      J2SDK 6.0.14

    • Important

    Description

      //struts.objectFactory.spring.autoWire.alwaysRespect works fine when true
      //Whether the autowire strategy should always be used, or the framework should try to guess the best strategy

      //code works perfect as long as alwaysRepect=true
      //analyzing alternative alwaysRepect=false nosedives into else guesswork code which may returns null

      com.opensymphony.xwork2.spring.SpringObjectFactory
      /*** @param clazz

      • @param extraContext
      • @throws Exception*/
        @Override
        public Object buildBean(Class clazz, Map<String, Object> extraContext) throws Exception {
        Object bean;
        try
        Unknown macro: {//follow autowire strategy or use 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 { //wrong if you want type!="name" // autoWiringFactory = findAutoWiringBeanFactory(this.appContext); only in setContext bean = autoWiringFactory.autowire(clazz, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); bean = autoWiringFactory.applyBeanPostProcessorsBeforeInitialization(bean, bean.getClass().getName()); //only defined in org.springframework.beans.factory.config.AutowireCapableBeanFactory // Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) // We don't need to call the init-method since one won't be registered. bean = autoWiringFactory.applyBeanPostProcessorsAfterInitialization(bean, bean.getClass().getName()); //only defined in org.springframework.beans.factory.config.AutowireCapableBeanFactory // Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) return autoWireBean(bean, autoWiringFactory); //returns NULL } }

        catch (UnsatisfiedDependencyException e)

        { if (LOG.isErrorEnabled()) LOG.error("Error building bean", e); // Fall back return autoWireBean(super.buildBean(clazz, extraContext), autoWiringFactory); }

        }

      public Object autoWireBean(Object bean, AutowireCapableBeanFactory autoWiringFactory) {
      if (autoWiringFactory != null)

      { autoWiringFactory.autowireBeanProperties(bean, autowireStrategy, false); }

      injectApplicationContext(bean);
      injectInternalBeans(bean);

      return bean;
      }

      com.opensymphony.xwork2.ObjectFactory
      /*** Build a generic Java object of the given type.

      • @param clazz the type of Object to build
      • @param extraContext a Map of extra context which uses the same keys as the {@link com.opensymphony.xwork2.ActionContext}

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

        { return clazz.newInstance(); }

      private void injectApplicationContext(Object bean) {
      if (bean instanceof ApplicationContextAware)

      { ((ApplicationContextAware) bean).setApplicationContext(appContext); }

      }
      /*** @param obj */
      protected Object injectInternalBeans(Object obj) {
      if (obj != null && container != null)

      { container.inject(obj); }

      return obj;
      }

      //a very good likelihood that the returned bean will be null in the else condition as it is never created

      Attachments

        Activity

          People

            Unassigned Unassigned
            mgainty Martin Gainty
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: