Spring Framework
  1. Spring Framework
  2. SPR-9270

Generic factory method and constructor-arg in Spring DI (xml)

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 3.0.7, 3.1.1
    • Fix Version/s: Waiting for Triage
    • Component/s: Core
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Using Spring IoC container and XML beans I've found bug - factory-method used on generic static method combined with <constructor-arg> and value="" attribute doesn't create bean. On the other hand if I use <value> element instead, it does the job.

      This bean works:

      <bean id="okSupplier" class="org.springframework.test.Suppliers" factory-method="ofInstance">
        <constructor-arg>
          <value type="java.lang.String">someValue</value>
        </constructor-arg>
      </bean>
      

      but this doesn't (with or without index="0"):

      <bean id="brokenSupplier" class="org.springframework.test.Suppliers" factory-method="ofInstance">
        <constructor-arg index="0" type="java.lang.String" value="someValue" />
      </bean>
      

      Instead an Exception is thrown:

      org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'brokenSupplier' defined in class path resource [brokenContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.Object]: Ambiguous factory method argument types - did you specify the correct bean references as factory method arguments?
      	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:716)
      	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:461)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1015)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
      	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
      	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
      	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
      	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
      	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
      	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
      	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
      	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
      	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
      	at org.springframework.test.AppTest.testBroken(AppTest.java:19)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      	at java.lang.reflect.Method.invoke(Method.java:597)
      	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
      	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
      	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
      	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
      	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
      	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
      	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
      	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
      	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
      	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
      	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
      	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
      	at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
      	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
      

      Issue described on Stackoverflow. Originally I use Google Guava library and its Supplier interface, but the bug can be reproduced on any generic static factory method.

      Maven project with tests is attached.

      1. spring-bug-test.tgz
        13 kB
        Grzegorz Rozniecki
      2. spring-bug-test-v2.tgz
        16 kB
        Grzegorz Rozniecki

        Activity

        Hide
        Grzegorz Rozniecki added a comment -

        I've looked into source and experimented a bit and it looks like specyfying type="some.Type" in <constructor-arg /> element changes this condition in ConstructorArgumentValues#getGenericArgumentValue(Class, String, Set<ValueHolder>) to true:

        if (valueHolder.getType() != null
            && (requiredType == null || !ClassUtils.matchesTypeName(requiredType, valueHolder.getType())))
        

        because requiredType is Object (T parameter in generic factory method) and valueHolder.getType() is String here, what casues UnsatisfiedDependencyException (thanks Yves!)

        Stack inside getGenericArgumentValue (on Spring 3.0.5):

        Thread [main] (Suspended)	
        	ConstructorArgumentValues.getGenericArgumentValue(Class, String, Set<ValueHolder>) line: 285	
        	ConstructorArgumentValues.getArgumentValue(int, Class, String, Set<ValueHolder>) line: 347	
        	ConstructorResolver.createArgumentArray(String, RootBeanDefinition, ConstructorArgumentValues, BeanWrapper, Class[], String[], Object, boolean) line: 664	
        	ConstructorResolver.instantiateUsingFactoryMethod(String, RootBeanDefinition, Object[]) line: 461	
        	DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).instantiateUsingFactoryMethod(String, RootBeanDefinition, Object[]) line: 1015	
        	DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBeanInstance(String, RootBeanDefinition, Object[]) line: 911	
        	DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean(String, RootBeanDefinition, Object[]) line: 485	
        	DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean(String, RootBeanDefinition, Object[]) line: 456	
        	AbstractBeanFactory$1.getObject() line: 294	
        	DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton(String, ObjectFactory) line: 225	
        	DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 291	
        	DefaultListableBeanFactory(AbstractBeanFactory).getBean(String) line: 193	
        	DefaultListableBeanFactory.preInstantiateSingletons() line: 585	
        	ClassPathXmlApplicationContext(AbstractApplicationContext).finishBeanFactoryInitialization(ConfigurableListableBeanFactory) line: 913	
        	ClassPathXmlApplicationContext(AbstractApplicationContext).refresh() line: 464	
        	ClassPathXmlApplicationContext.<init>(String[], boolean, ApplicationContext) line: 139	
        	ClassPathXmlApplicationContext.<init>(String) line: 83	
        	AppTest.testBase(String, String, Object) line: 30	
        	AppTest.testBroken() line: 16	
        	NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]	
        	NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39	
        	DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25	
        	Method.invoke(Object, Object...) line: 597	
        	FrameworkMethod$1.runReflectiveCall() line: 45	
        	FrameworkMethod$1(ReflectiveCallable).run() line: 15	
        	FrameworkMethod.invokeExplosively(Object, Object...) line: 42	
        	InvokeMethod.evaluate() line: 20	
        	BlockJUnit4ClassRunner(ParentRunner<T>).runLeaf(Statement, Description, RunNotifier) line: 263	
        	BlockJUnit4ClassRunner.runChild(FrameworkMethod, RunNotifier) line: 68	
        	BlockJUnit4ClassRunner.runChild(Object, RunNotifier) line: 47	
        	ParentRunner$3.run() line: 231	
        	ParentRunner$1.schedule(Runnable) line: 60	
        	BlockJUnit4ClassRunner(ParentRunner<T>).runChildren(RunNotifier) line: 229	
        	ParentRunner<T>.access$000(ParentRunner, RunNotifier) line: 50	
        	ParentRunner$2.evaluate() line: 222	
        	BlockJUnit4ClassRunner(ParentRunner<T>).run(RunNotifier) line: 300	
        	JUnit4TestMethodReference(JUnit4TestReference).run(TestExecution) line: 50	
        	TestExecution.run(ITestReference[]) line: 38	
        	RemoteTestRunner.runTests(String[], String, TestExecution) line: 467	
        	RemoteTestRunner.runTests(TestExecution) line: 683	
        	RemoteTestRunner.run() line: 390	
        	RemoteTestRunner.main(String[]) line: 197	
        
        Show
        Grzegorz Rozniecki added a comment - I've looked into source and experimented a bit and it looks like specyfying type="some.Type" in <constructor-arg /> element changes this condition in ConstructorArgumentValues#getGenericArgumentValue(Class, String, Set<ValueHolder>) to true: if (valueHolder.getType() != null && (requiredType == null || !ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) because requiredType is Object (T parameter in generic factory method) and valueHolder.getType() is String here, what casues UnsatisfiedDependencyException ( thanks Yves !) — Stack inside getGenericArgumentValue (on Spring 3.0.5): Thread [main] (Suspended) ConstructorArgumentValues.getGenericArgumentValue( Class , String , Set<ValueHolder>) line: 285 ConstructorArgumentValues.getArgumentValue( int , Class , String , Set<ValueHolder>) line: 347 ConstructorResolver.createArgumentArray( String , RootBeanDefinition, ConstructorArgumentValues, BeanWrapper, Class [], String [], Object , boolean ) line: 664 ConstructorResolver.instantiateUsingFactoryMethod( String , RootBeanDefinition, Object []) line: 461 DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).instantiateUsingFactoryMethod( String , RootBeanDefinition, Object []) line: 1015 DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBeanInstance( String , RootBeanDefinition, Object []) line: 911 DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean( String , RootBeanDefinition, Object []) line: 485 DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean( String , RootBeanDefinition, Object []) line: 456 AbstractBeanFactory$1.getObject() line: 294 DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton( String , ObjectFactory) line: 225 DefaultListableBeanFactory(AbstractBeanFactory).doGetBean( String , Class <T>, Object [], boolean ) line: 291 DefaultListableBeanFactory(AbstractBeanFactory).getBean( String ) line: 193 DefaultListableBeanFactory.preInstantiateSingletons() line: 585 ClassPathXmlApplicationContext(AbstractApplicationContext).finishBeanFactoryInitialization(ConfigurableListableBeanFactory) line: 913 ClassPathXmlApplicationContext(AbstractApplicationContext).refresh() line: 464 ClassPathXmlApplicationContext.<init>( String [], boolean , ApplicationContext) line: 139 ClassPathXmlApplicationContext.<init>( String ) line: 83 AppTest.testBase( String , String , Object ) line: 30 AppTest.testBroken() line: 16 NativeMethodAccessorImpl.invoke0(Method, Object , Object []) line: not available [ native method] NativeMethodAccessorImpl.invoke( Object , Object []) line: 39 DelegatingMethodAccessorImpl.invoke( Object , Object []) line: 25 Method.invoke( Object , Object ...) line: 597 FrameworkMethod$1.runReflectiveCall() line: 45 FrameworkMethod$1(ReflectiveCallable).run() line: 15 FrameworkMethod.invokeExplosively( Object , Object ...) line: 42 InvokeMethod.evaluate() line: 20 BlockJUnit4ClassRunner(ParentRunner<T>).runLeaf(Statement, Description, RunNotifier) line: 263 BlockJUnit4ClassRunner.runChild(FrameworkMethod, RunNotifier) line: 68 BlockJUnit4ClassRunner.runChild( Object , RunNotifier) line: 47 ParentRunner$3.run() line: 231 ParentRunner$1.schedule( Runnable ) line: 60 BlockJUnit4ClassRunner(ParentRunner<T>).runChildren(RunNotifier) line: 229 ParentRunner<T>.access$000(ParentRunner, RunNotifier) line: 50 ParentRunner$2.evaluate() line: 222 BlockJUnit4ClassRunner(ParentRunner<T>).run(RunNotifier) line: 300 JUnit4TestMethodReference(JUnit4TestReference).run(TestExecution) line: 50 TestExecution.run(ITestReference[]) line: 38 RemoteTestRunner.runTests( String [], String , TestExecution) line: 467 RemoteTestRunner.runTests(TestExecution) line: 683 RemoteTestRunner.run() line: 390 RemoteTestRunner.main( String []) line: 197

          People

          • Assignee:
            Unassigned
            Reporter:
            Grzegorz Rozniecki
            Last updater:
            Chris Beams
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Days since last comment:
              2 years, 4 weeks, 1 day ago