Skip to content

Invalid instance injected for generic type in case of partial type variable [SPR-16179] #20727

Closed
@spring-projects-issues

Description

@spring-projects-issues

Oliver Drotbohm opened SPR-16179 and commented

The following test fails:

package example;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SpringInjectionTest {

	@Configuration
	static class Config {

		@Bean
		PageAssembler<?> assembler() {
			return new PageAssembler<>();
		}
	}

	@Autowired(required = false) Assembler<SomeOtherType> assembler;

	@Test
	public void testname() {
		assertThat(assembler, is(nullValue()));
	}

	interface Assembler<T> {}

	static class PageAssembler<T> implements Assembler<Page<T>> {}

	interface Page<T> {}

	interface SomeOtherType {}
}

Spring injects the configured bean instance here despite the fact that the generic declarations clearly don't match as Page<T> is not compatible with the requested SomeOtherType.


Affects: 4.3.12, 5.0.1

Referenced from: commits 59d654b, e2bb06e

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions