Skip to content

FactoryBean bean type detection can causes fatal early instantiation [SPR-11202] #15828

Closed
@spring-projects-issues

Description

@spring-projects-issues

Dave Syer opened SPR-11202 and commented

The BeanFactory.getBeanNamesForAnnotation() method has been causing issues. Here's a test case:

public class BeansWithAnnotationTests {

	private AnnotationConfigApplicationContext context;

	@After
	public void close() {
		if (context != null) {
			context.close();
		}
	}

	@Test // Fails
	public void testWithImporter() {
		context = new AnnotationConfigApplicationContext(Wrapper.class);
		assertEquals("foo", context.getBean("value"));
	}
	
	@Test // Passes
	public void testWithoutImporter() {
		context = new AnnotationConfigApplicationContext(Config.class);
		assertEquals("foo", context.getBean("value"));
	}
	
	@Configuration
	@Import(Selector.class)
	protected static class Wrapper {
		
	}
	
	protected static class Selector implements ImportSelector {

		@Override
		public String[] selectImports(AnnotationMetadata importingClassMetadata) {
			return new String[] {Config.class.getName() };
		}
		
	}

	@Configuration
	protected static class Config {

		public Config() {
			// Just so I can put a breakpoint here
			System.getProperty("foo");
		}

		@Bean
		public FooFactoryBean foo() {
			return new FooFactoryBean();
		}

		@Bean
		public String value() throws Exception {
			String name = foo().getObject().getName();
			Assert.state(name != null, "Name cannot be null");
			return name;
		}

		@Bean
		@Conditional(NoBarCondition.class)
		public String bar() throws Exception {
			return "bar";
		}

	}

	protected static class NoBarCondition implements Condition {

		@Override
		public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
			if (context.getBeanFactory().getBeanNamesForAnnotation(Bar.class).length > 0) {
				return false;
			}
			return true;
		}

	}

	@Retention(RetentionPolicy.RUNTIME)
	@Documented
	@Target(ElementType.TYPE)
	protected static @interface Bar {
	}

	protected static class FooFactoryBean implements FactoryBean<Foo>, InitializingBean {

		private Foo foo = new Foo();

		@Override
		public Foo getObject() throws Exception {
			return foo;
		}

		@Override
		public Class<?> getObjectType() {
			return Foo.class;
		}

		@Override
		public boolean isSingleton() {
			return true;
		}

		@Override
		public void afterPropertiesSet() throws Exception {
			this.foo.name = "foo";
		}

	}

	protected static class Foo {

		private String name;

		public String getName() {
			return name;
		}

	}

}

You need a perfect storm of a FactoryBean as a @Bean inside a @Configuration that was ImportSelected for this to break (I tried quite a few other scenarios). The passing test works because Spring doesn't try to instantiate the Config object.


Affects: 4.0 RC2

Issue Links:

Referenced from: commits 106a973

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