Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autowiring unable to find generic dependency [SPR-11529] #16154

Closed
spring-projects-issues opened this issue Mar 7, 2014 · 2 comments
Closed

Autowiring unable to find generic dependency [SPR-11529] #16154

spring-projects-issues opened this issue Mar 7, 2014 · 2 comments
Assignees
Labels
in: core type: regression
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Mar 7, 2014

Jair da Silva Ferreira Júnior opened SPR-11529 and commented

Hi,
I believe I found an issue with autowiring and generics.
I can't really tell if this issue is a duplicate of #16096, so I decided to report it anyway. The main diference with this issue is that I use the @Service annotation instead of a factory method to create the GenericInterface1Impl spring bean.
I attached a simple eclipse example project to show you the issue.

Here is my "application-context.xml" file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<context:component-scan base-package="spring.test.generics2"/>
	
</beans>

I have 2 classes: "GenericInterface1Impl" and "GenericInterface2Impl".
"GenericInterface1Impl" depends on an implementation of "GenericInterface2".

@Service
public class GenericInterface1Impl<T> implements GenericInterface1<T>{

	@Autowired
	private GenericInterface2<T> gi2;

	@Override
	public String doSomethingGeneric(T o) {
		return gi2.doSomethingMoreGeneric(o) + "_somethingGeneric_" + o;
	}
	
}
@Service
public class GenericInterface2Impl implements GenericInterface2<String>{

	@Override
	public String doSomethingMoreGeneric(String o) {
		return "somethingMoreGeneric_" + o;
	}
}

The issue is: spring can't find a qualifying bean for the attribute "GenericInterface1Impl.gi2" even though there's a suitable "GenericInterface2" implementation (GenericInterface2Impl) in the application context.

The exact same code works perfectly with spring version 3.2.8 but fails with spring version 4.0.2. You can easily change the spring version to 3.2.8 in "pom.xml" to verify that.

Here's the exception stack trace:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'genericInterface1Impl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private spring.test.generics2.GenericInterface2 spring.test.generics2.GenericInterface1Impl.gi2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [spring.test.generics2.GenericInterface2] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at spring.test.generics2.App.main(App.java:13)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private spring.test.generics2.GenericInterface2 spring.test.generics2.GenericInterface1Impl.gi2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [spring.test.generics2.GenericInterface2] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
	... 13 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [spring.test.generics2.GenericInterface2] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
	... 15 more


Affects: 4.0.2

Attachments:

Issue Links:

  • #16096 Autowiring unable to find generic dependency in case of nested unresolved type variable
@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Mar 8, 2014

Juergen Hoeller commented

This is indeed fixed along with #16096, and related to my comment there, it's even more comprehensively fixed for the present case since you're not using factory methods, so there's no type variable substitution to consider at the factory method level (which #16146 deals with).

For both fixes, please give the latest 4.0.3 snapshot a try (see http://projects.spring.io/spring-framework/ for Maven coordinates) and let us know whether it works for you...

Juergen

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Mar 8, 2014

Jair da Silva Ferreira Júnior commented

Thank you very much!
It works now!

Jair

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core type: regression
Projects
None yet
Development

No branches or pull requests

2 participants