tags:

views:

59

answers:

2

Hi

I am facing problem in ProxyFactoryBean class,

We want to get the class name of the targetBean of the ProxyFactoryBean .

When we invoke getType on BeanFactory giving the bean name , it always return as null.

Our Java code is

public class TestSpring {
   public static void main(String args[]){    
     TestSpring ts = new TestSpring();
     ts.process();
   }

   private void process() {
     BeanFactory factory = new XmlBeanFactory(new FileSystemResource("E:\\beans.xml"));
     Class c = factory.getType("scor.imagedev.action.imageDevServerTaskActions");
     System.out.println(c);
   }
}

Our configuration file is as follows:

<bean id="scor.actionProxyTemplate" class="org.springframework.aop.framework.ProxyFactoryBean"  abstract="true" >
   <property name="proxyTargetClass" value="true" />    
</bean>

 <bean id="scor.imagedev.action.imageDevServerTaskActions" parent="scor.actionProxyTemplate" scope="prototype">    
   <property name="target"> 
     <bean class="test.spring.Foo"/>
   </property>
 </bean>

Some of the other things that i want to add here.

  1. If we make the bean as singleton , it works. But in our case we want it to be a prototype.
  2. We have to use BeanFactory.getType(<beanName>). This is our base framework make a call. We cannot change in our base framework.
  3. Will targetSource can solve the problem? I tried it but it is of no use. May be I used it incorrectly
  4. I am using Spring 2.0.6.

Regards Ankit

A: 

The documentation says that the method returns null if the type is not determinable. I couldn't find reference of what that means, but given the symptoms I would assume that it is not determinable because of the prototype scope. (The bean exists, because otherwise an exception would be thrown)

After looking at the source code, I don't see a reason for returning null - If there is no singleton, the bean definitions are consulted. What I can suggest is to try to temporarily get spring 2.5.6 (or 3.0.x) and see if the issue can be reproduced, or is fixed.

Bozho
@Bozho Thanks for the reply. I did tried with 2.5.6 and 3.0 . I am getting the same result. I did debug the Spring Source code. I somehow smell a problem here. For my case call is going in AbstractAutowireCapableBeanFactory.getNonSingletonFactoryBeanForType Check . Here it is creating a basic ProxyFactoryBean class with no properties injected. And after this object is created , a call is given to getObjectType of factory bean . Here it is looking for targetClass to get the object type. But since targetClass is not injected , it returns null.
Ankit
@Ankit report it as a bug. They'll verify it and give more insight.
Bozho
A: 

I am facing the same problem, and while debugging i found, getTypeForFactoryBean function in AbstractBeanFactory is not working as properly. This function returning NULL for a protoType bean.

/**
     * Determine the bean type for the given FactoryBean definition, as far as possible.
     * Only called if there is no singleton instance registered for the target bean already.
     * <p>The default implementation creates the FactoryBean via <code>getBean</code>
     * to call its <code>getObjectType</code> method. Subclasses are encouraged to optimize
/
protected Class getTypeForFactoryBean(String beanName, RootBeanDefinition mbd) {
            if (!mbd.isSingleton()) {
                return null;
            }
            try {
                FactoryBean factoryBean = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName);
                return getTypeForFactoryBean(factoryBean);
            }
            catch (BeanCreationException ex) {
                // Can only happen when getting a FactoryBean.
                logger.debug("Ignoring bean creation exception on FactoryBean type check", ex);
                return null;
            }
        }

Ideally above function should call getObjectType on ProxyFactoryInstance, but due to first check for singleton above function is returning NULL.

coder
@coder thanks. I have logged the issue in JIRA https://jira.springsource.org/browse/SPR-7615
Ankit