|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
* Copyright 2002-2015 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -19,13 +19,17 @@ package org.springframework.context.annotation; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.FactoryBean; |
|
|
|
import org.springframework.beans.factory.FactoryBean; |
|
|
|
|
|
|
|
import org.springframework.beans.factory.InitializingBean; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Tests cornering bug SPR-8514. |
|
|
|
* Tests cornering bug SPR-8514. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Chris Beams |
|
|
|
* @author Chris Beams |
|
|
|
|
|
|
|
* @author Juergen Hoeller |
|
|
|
* @since 3.1 |
|
|
|
* @since 3.1 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class ConfigurationWithFactoryBeanAndAutowiringTests { |
|
|
|
public class ConfigurationWithFactoryBeanAndAutowiringTests { |
|
|
|
@ -77,30 +81,52 @@ public class ConfigurationWithFactoryBeanAndAutowiringTests { |
|
|
|
ctx.register(WildcardParameterizedFactoryBeanInterfaceConfig.class); |
|
|
|
ctx.register(WildcardParameterizedFactoryBeanInterfaceConfig.class); |
|
|
|
ctx.refresh(); |
|
|
|
ctx.refresh(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void withFactoryBeanCallingBean() { |
|
|
|
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
|
|
|
|
|
|
|
ctx.register(AppConfig.class); |
|
|
|
|
|
|
|
ctx.register(FactoryBeanCallingConfig.class); |
|
|
|
|
|
|
|
ctx.refresh(); |
|
|
|
|
|
|
|
assertEquals("true", ctx.getBean("myString")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
class DummyBean { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class DummyBean { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class MyFactoryBean implements FactoryBean<String>, InitializingBean { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean initialized = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void afterPropertiesSet() throws Exception { |
|
|
|
|
|
|
|
this.initialized = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
class MyFactoryBean implements FactoryBean<String> { |
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String getObject() throws Exception { |
|
|
|
public String getObject() throws Exception { |
|
|
|
return "foo"; |
|
|
|
return "foo"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Class<String> getObjectType() { |
|
|
|
public Class<String> getObjectType() { |
|
|
|
return String.class; |
|
|
|
return String.class; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean isSingleton() { |
|
|
|
public boolean isSingleton() { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getString() { |
|
|
|
|
|
|
|
return Boolean.toString(this.initialized); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MyParameterizedFactoryBean<T> implements FactoryBean<T> { |
|
|
|
static class MyParameterizedFactoryBean<T> implements FactoryBean<T> { |
|
|
|
|
|
|
|
|
|
|
|
private final T obj; |
|
|
|
private final T obj; |
|
|
|
|
|
|
|
|
|
|
|
@ -123,20 +149,22 @@ class MyParameterizedFactoryBean<T> implements FactoryBean<T> { |
|
|
|
public boolean isSingleton() { |
|
|
|
public boolean isSingleton() { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
class AppConfig { |
|
|
|
static class AppConfig { |
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
@Bean |
|
|
|
public DummyBean dummyBean() { |
|
|
|
public DummyBean dummyBean() { |
|
|
|
return new DummyBean(); |
|
|
|
return new DummyBean(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
static class ConcreteFactoryBeanImplementationConfig { |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
class ConcreteFactoryBeanImplementationConfig { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private DummyBean dummyBean; |
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
@ -145,11 +173,12 @@ class ConcreteFactoryBeanImplementationConfig { |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
return new MyFactoryBean(); |
|
|
|
return new MyFactoryBean(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
static class ParameterizedFactoryBeanImplementationConfig { |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
class ParameterizedFactoryBeanImplementationConfig { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private DummyBean dummyBean; |
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
@ -158,11 +187,12 @@ class ParameterizedFactoryBeanImplementationConfig { |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
return new MyParameterizedFactoryBean<String>("whatev"); |
|
|
|
return new MyParameterizedFactoryBean<String>("whatev"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
class ParameterizedFactoryBeanInterfaceConfig { |
|
|
|
static class ParameterizedFactoryBeanInterfaceConfig { |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private DummyBean dummyBean; |
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
@ -171,11 +201,12 @@ class ParameterizedFactoryBeanInterfaceConfig { |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
return new MyFactoryBean(); |
|
|
|
return new MyFactoryBean(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
static class NonPublicParameterizedFactoryBeanInterfaceConfig { |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
class NonPublicParameterizedFactoryBeanInterfaceConfig { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private DummyBean dummyBean; |
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
@ -184,11 +215,12 @@ class NonPublicParameterizedFactoryBeanInterfaceConfig { |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
return new MyFactoryBean(); |
|
|
|
return new MyFactoryBean(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
class RawFactoryBeanInterfaceConfig { |
|
|
|
static class RawFactoryBeanInterfaceConfig { |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private DummyBean dummyBean; |
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
@ -198,11 +230,12 @@ class RawFactoryBeanInterfaceConfig { |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
return new MyFactoryBean(); |
|
|
|
return new MyFactoryBean(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
static class WildcardParameterizedFactoryBeanInterfaceConfig { |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
class WildcardParameterizedFactoryBeanInterfaceConfig { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private DummyBean dummyBean; |
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
@ -211,4 +244,25 @@ class WildcardParameterizedFactoryBeanInterfaceConfig { |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
return new MyFactoryBean(); |
|
|
|
return new MyFactoryBean(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
static class FactoryBeanCallingConfig { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
|
|
|
public MyFactoryBean factoryBean() { |
|
|
|
|
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
|
|
|
|
return new MyFactoryBean(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
|
|
|
public String myString() { |
|
|
|
|
|
|
|
return factoryBean().getString(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|