Prevent early FactoryBean creation for type checking purposes when coming from a factory method on yet another bean (e.g. from a configuration class)
Issue: SPR-11202
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class Spr11202Tests {
|
||||
|
||||
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 {
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -80,6 +80,7 @@ public class ConfigurationClassProcessingTests {
|
||||
RequiredAnnotationBeanPostProcessor rapp = new RequiredAnnotationBeanPostProcessor();
|
||||
rapp.setBeanFactory(factory);
|
||||
factory.addBeanPostProcessor(rapp);
|
||||
factory.freezeConfiguration();
|
||||
return factory;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user