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

Cannot inject with qualifier into wildcard collection #26522

Closed
hban opened this issue Feb 8, 2021 · 3 comments
Closed

Cannot inject with qualifier into wildcard collection #26522

hban opened this issue Feb 8, 2021 · 3 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: superseded An issue that has been superseded by another

Comments

@hban
Copy link

hban commented Feb 8, 2021

Affects: 5.3.3


Injecting by qualifier doesn't work when target collection is declared as List<? extends Object>, which is how Kotlin translates List<Any>.

Reproducer:

package example;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;

@Configuration
@ComponentScan
public class ExampleApplication {
    public static void main(String[] args) {
        new AnnotationConfigApplicationContext(ExampleApplication.class);
    }
}

@Component
@Qualifier("EXAMPLE")
class Foo {}

@Component
class Bar {
    public Bar(
        @Qualifier("EXAMPLE") List<Object> good,
        @Qualifier("EXAMPLE") List<? extends Object> bad
    ) {
        System.out.println("Good " + good);
        System.out.println("Bad: " + bad);
    }
}

Output:

Good [example.Foo@5552768b]
Bad: []
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 8, 2021
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Feb 8, 2021
@ooooo-youwillsee
Copy link

This doesn't seem like Spring's bug, I find a simple Java Demo

class Bar {
	
	public Bar(
			@Qualifier("EXAMPLE") List<? extends Foo> p1,
			@Qualifier("EXAMPLE") List<? extends Object> p2,
			@Qualifier("EXAMPLE") List<?> p3
	) {
		System.out.println("p1: " + p1);
		System.out.println("p2: " + p2);
		System.out.println("p3: " + p3);
	}
	
	public static void main(String[] args) {
		Constructor<?> constructor = Bar.class.getConstructors()[0];
		Type[] genericParameterTypes = constructor.getGenericParameterTypes();
		for (Type parameterType : genericParameterTypes) {
			if (parameterType instanceof ParameterizedType) {
				System.out.println(parameterType);
			}
		}
	}
	
}

output

java.util.List<? extends com.ooooo.Foo>
java.util.List<?>
java.util.List<?>

In fact, ? extend Object seem to equals ?.

I found an interesting part Collection.class.isAssignableFrom(type) && type.isInterface() for resolve collection in org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveMultipleBeans method, ? as ResolvableType will return null in spring.

@hban
Copy link
Author

hban commented Nov 22, 2021

I apologize if pinging is frowned upon here, but could someone take a look at this issue? It has been waiting for triage for several months now.

If it might help, there was a somewhat similar issue I opened for Spring Boot before (spring-projects/spring-boot#18767), which was fixed. Maybe the cause is same as for this issue?

@hban hban closed this as not planned Won't fix, can't repro, duplicate, stale Jul 7, 2022
@sbrannen
Copy link
Member

Superseded by #24145

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Dec 13, 2022
@sbrannen sbrannen added status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: superseded An issue that has been superseded by another
Projects
None yet
Development

No branches or pull requests

4 participants