Announcement Announcement Module
Collapse
No announcement yet.
Trouble with "BeanFactory".getBean(bean, args[]) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trouble with "BeanFactory".getBean(bean, args[])

    I'm having issues getting the getBean(string, object[]) method of the bean factory to work correctly. Basically what I'm trying to do is setup a proxy factory that returns prototype instances with arguments that are passed in.

    I think I got the xml correct, but I get the following error:
    Error creating bean with name 'myBean' defined in class path resource [beans.xml]: 8 constructor arguments specified but no matching constructor found in bean 'myBean' (hint: specify index and/or type arguments for simple parameters to avoid type ambiguities)

    Here is a snippet from my XML:
    Code:
    <bean id="aopLogger" class="LoggingAspect" />
    <bean id="myBean" class="org.springframework.aop.framework.ProxyFactoryBean"
    		scope="prototype">
        <property name="proxyInterfaces"><value>MyBeanInterface</value></property>
        <property name="target">
            <bean id="myBeanFactory" class="...MyBeanFactory"	scope="prototype" factory-method="createMyBean">
                <constructor-arg index="0"><value>""</value></constructor-arg>
                <constructor-arg index="1"><value>""</value></constructor-arg>
                <constructor-arg index="2"><value>""</value></constructor-arg>
                <constructor-arg index="3"><value>null</value></constructor-arg>
                <constructor-arg index="4"><list></list></constructor-arg>
                <constructor-arg index="5"><value>""</value></constructor-arg>
                <constructor-arg index="6"><value>""</value></constructor-arg>
                <constructor-arg index="7"><value>""</value></constructor-arg>
            </bean>
        </property>
        <property name="interceptorNames">
            <list>
                <value>aopLogger</value>
            </list>
        </property>
    </bean>
    here is the MyBeanFactory Class
    Code:
    public class MyBeanFactory
    {
        public static MyBeanInterface createMyBean(UUID id, String name, 
                             String description, IVersion version,                                              
                             List<IDependency> dependencies, String company,
                             String url, String copyright)
        {
            return new MyBeanImpl(id, name, description, version, 
                                            dependencies, company, url, copyright);
        }
    }
    and the code that calls it:
    Code:
    return (MyBeanImpl) context.getBean("myBean", 
               new Object[] { id, name, description, version, dependencies, 
               company, url, copyright });
    I'm a little confused here... The method takes 8 args, I've used index to specify which ones are which, but it still gives me that error. I have also tried to identify the types, but that didn't do anything. I've also tried to create a reference of the bean directly without using my own factory class, but had the same issues with that.

    Any ideas?

    Thanks.
    Last edited by davija; Jul 27th, 2009, 07:35 PM.

  • #2
    1. 'myBean' class is ProxyFactoryBean, so, when you use 'context.getBean("myBean", new Object[] { id, name, description, version, dependencies, company, url, copyright })' that means that you're trying to call ProxyFactoryBean constructor with that arguments. There is no such a constructor, so, you have the mentioned problem;
    2. There is no point in using 'prototype' scope for the 'myBeanFactory' bean because its outer bean ('myBean') is already defined as a prototype;
    3. You defined the first 'myBeanFactory' argument as string - that's incorrect, because string is not UUID;
    4. You incorrectly inject null ('<constructor-arg index="3"><value>null</value></constructor-arg>'). Right way is '<constructor-arg index="3"><null/></constructor-arg>' - 3.3.2.5. Nulls;

    I'd suggest you to use not ProxyFactoryBean but spring2 aop setup - 6. Aspect Oriented Programming with Spring. That eliminates the problem, i.e. you just define 'myBean' as a prototype MyBeanFactory.createMyBean() and let aop infrastructure make necessary on bean request.

    Comment


    • #3
      Originally posted by denis.zhdanov View Post
      1. 'myBean' class is ProxyFactoryBean, so, when you use 'context.getBean("myBean", new Object[] { id, name, description, version, dependencies, company, url, copyright })' that means that you're trying to call ProxyFactoryBean constructor with that arguments. There is no such a constructor, so, you have the mentioned problem;
      2. There is no point in using 'prototype' scope for the 'myBeanFactory' bean because its outer bean ('myBean') is already defined as a prototype;
      3. You defined the first 'myBeanFactory' argument as string - that's incorrect, because string is not UUID;
      4. You incorrectly inject null ('<constructor-arg index="3"><value>null</value></constructor-arg>'). Right way is '<constructor-arg index="3"><null/></constructor-arg>' - 3.3.2.5. Nulls;

      I'd suggest you to use not ProxyFactoryBean but spring2 aop setup - 6. Aspect Oriented Programming with Spring. That eliminates the problem, i.e. you just define 'myBean' as a prototype MyBeanFactory.createMyBean() and let aop infrastructure make necessary on bean request.

      This all makes sense, I don't know why I didn't catch it yesterday when looking at it. I'll try these suggestions and see what happens. Thanks for the quick reply.

      Comment


      • #4
        Thanks for the help, this works now with the suggested changes.

        Comment

        Working...
        X