|
|
|
@ -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,138 +81,188 @@ public class ConfigurationWithFactoryBeanAndAutowiringTests { |
|
|
|
ctx.register(WildcardParameterizedFactoryBeanInterfaceConfig.class); |
|
|
|
ctx.register(WildcardParameterizedFactoryBeanInterfaceConfig.class); |
|
|
|
ctx.refresh(); |
|
|
|
ctx.refresh(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DummyBean { |
|
|
|
@Test |
|
|
|
} |
|
|
|
public void withFactoryBeanCallingBean() { |
|
|
|
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
|
|
|
|
|
|
|
ctx.register(AppConfig.class); |
|
|
|
|
|
|
|
ctx.register(FactoryBeanCallingConfig.class); |
|
|
|
|
|
|
|
ctx.refresh(); |
|
|
|
|
|
|
|
assertEquals("true", ctx.getBean("myString")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MyFactoryBean implements FactoryBean<String> { |
|
|
|
static class DummyBean { |
|
|
|
@Override |
|
|
|
|
|
|
|
public String getObject() throws Exception { |
|
|
|
|
|
|
|
return "foo"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public Class<String> getObjectType() { |
|
|
|
|
|
|
|
return String.class; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean isSingleton() { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MyParameterizedFactoryBean<T> implements FactoryBean<T> { |
|
|
|
static class MyFactoryBean implements FactoryBean<String>, InitializingBean { |
|
|
|
|
|
|
|
|
|
|
|
private final T obj; |
|
|
|
private boolean initialized = false; |
|
|
|
|
|
|
|
|
|
|
|
public MyParameterizedFactoryBean(T obj) { |
|
|
|
@Override |
|
|
|
this.obj = obj; |
|
|
|
public void afterPropertiesSet() throws Exception { |
|
|
|
} |
|
|
|
this.initialized = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public String getObject() throws Exception { |
|
|
|
|
|
|
|
return "foo"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public Class<String> getObjectType() { |
|
|
|
|
|
|
|
return String.class; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean isSingleton() { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public String getString() { |
|
|
|
public T getObject() throws Exception { |
|
|
|
return Boolean.toString(this.initialized); |
|
|
|
return obj; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
static class MyParameterizedFactoryBean<T> implements FactoryBean<T> { |
|
|
|
public Class<T> getObjectType() { |
|
|
|
|
|
|
|
return (Class<T>)obj.getClass(); |
|
|
|
private final T obj; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public MyParameterizedFactoryBean(T obj) { |
|
|
|
|
|
|
|
this.obj = obj; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public T getObject() throws Exception { |
|
|
|
|
|
|
|
return obj; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
|
|
|
public Class<T> getObjectType() { |
|
|
|
|
|
|
|
return (Class<T>)obj.getClass(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean isSingleton() { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean isSingleton() { |
|
|
|
@Configuration |
|
|
|
return true; |
|
|
|
static class AppConfig { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
|
|
|
public DummyBean dummyBean() { |
|
|
|
|
|
|
|
return new DummyBean(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
class AppConfig { |
|
|
|
static class ConcreteFactoryBeanImplementationConfig { |
|
|
|
@Bean |
|
|
|
|
|
|
|
public DummyBean dummyBean() { |
|
|
|
@Autowired |
|
|
|
return new DummyBean(); |
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
|
|
|
public MyFactoryBean factoryBean() { |
|
|
|
|
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
|
|
|
|
return new MyFactoryBean(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
class ConcreteFactoryBeanImplementationConfig { |
|
|
|
static class ParameterizedFactoryBeanImplementationConfig { |
|
|
|
@Autowired |
|
|
|
|
|
|
|
private DummyBean dummyBean; |
|
|
|
@Autowired |
|
|
|
|
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
@Bean |
|
|
|
public MyFactoryBean factoryBean() { |
|
|
|
public MyParameterizedFactoryBean<String> factoryBean() { |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
return new MyFactoryBean(); |
|
|
|
return new MyParameterizedFactoryBean<String>("whatev"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
class ParameterizedFactoryBeanImplementationConfig { |
|
|
|
static class ParameterizedFactoryBeanInterfaceConfig { |
|
|
|
@Autowired |
|
|
|
|
|
|
|
private DummyBean dummyBean; |
|
|
|
@Autowired |
|
|
|
|
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
@Bean |
|
|
|
public MyParameterizedFactoryBean<String> factoryBean() { |
|
|
|
public FactoryBean<String> factoryBean() { |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
return new MyParameterizedFactoryBean<String>("whatev"); |
|
|
|
return new MyFactoryBean(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
class ParameterizedFactoryBeanInterfaceConfig { |
|
|
|
static class NonPublicParameterizedFactoryBeanInterfaceConfig { |
|
|
|
@Autowired |
|
|
|
|
|
|
|
private DummyBean dummyBean; |
|
|
|
@Autowired |
|
|
|
|
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
@Bean |
|
|
|
public FactoryBean<String> factoryBean() { |
|
|
|
FactoryBean<String> factoryBean() { |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
return new MyFactoryBean(); |
|
|
|
return new MyFactoryBean(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
class NonPublicParameterizedFactoryBeanInterfaceConfig { |
|
|
|
static class RawFactoryBeanInterfaceConfig { |
|
|
|
@Autowired |
|
|
|
|
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
@Autowired |
|
|
|
FactoryBean<String> factoryBean() { |
|
|
|
private DummyBean dummyBean; |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
|
|
|
|
return new MyFactoryBean(); |
|
|
|
@Bean |
|
|
|
|
|
|
|
@SuppressWarnings("rawtypes") |
|
|
|
|
|
|
|
public FactoryBean factoryBean() { |
|
|
|
|
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
|
|
|
|
return new MyFactoryBean(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
class RawFactoryBeanInterfaceConfig { |
|
|
|
static class WildcardParameterizedFactoryBeanInterfaceConfig { |
|
|
|
@Autowired |
|
|
|
|
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
@Autowired |
|
|
|
@SuppressWarnings("rawtypes") |
|
|
|
private DummyBean dummyBean; |
|
|
|
public FactoryBean factoryBean() { |
|
|
|
|
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
@Bean |
|
|
|
return new MyFactoryBean(); |
|
|
|
public FactoryBean<?> factoryBean() { |
|
|
|
|
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
|
|
|
|
return new MyFactoryBean(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
class WildcardParameterizedFactoryBeanInterfaceConfig { |
|
|
|
static class FactoryBeanCallingConfig { |
|
|
|
@Autowired |
|
|
|
|
|
|
|
private DummyBean dummyBean; |
|
|
|
@Autowired |
|
|
|
|
|
|
|
private DummyBean dummyBean; |
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
@Bean |
|
|
|
public FactoryBean<?> factoryBean() { |
|
|
|
public MyFactoryBean factoryBean() { |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
Assert.notNull(dummyBean, "DummyBean was not injected."); |
|
|
|
return new MyFactoryBean(); |
|
|
|
return new MyFactoryBean(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
|
|
|
public String myString() { |
|
|
|
|
|
|
|
return factoryBean().getString(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|