BeanMethodInterceptor forwards user-provided arguments to getBean(name, args)

Issue: SPR-12443
This commit is contained in:
Juergen Hoeller 2014-11-22 16:06:25 +01:00
parent 75c70fac3d
commit 4bd75e4146
3 changed files with 82 additions and 32 deletions

View File

@ -45,6 +45,7 @@ import org.springframework.cglib.transform.ClassEmitterTransformer;
import org.springframework.cglib.transform.TransformingClassGenerator;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
/**
@ -321,7 +322,8 @@ class ConfigurationClassEnhancer {
if (alreadyInCreation) {
beanFactory.setCurrentlyInCreation(beanName, false);
}
return beanFactory.getBean(beanName);
return (!ObjectUtils.isEmpty(beanMethodArgs) ?
beanFactory.getBean(beanName, beanMethodArgs) : beanFactory.getBean(beanName));
}
finally {
if (alreadyInCreation) {

View File

@ -468,6 +468,12 @@ public class ConfigurationClassPostProcessorTests {
}
}
@Test
public void testPrototypeArgumentsThroughBeanMethodCall() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(BeanArgumentConfig.class);
ctx.getBean(FooFactory.class).createFoo(new BarArgument());
}
// -------------------------------------------------------------------------
@ -925,4 +931,38 @@ public class ConfigurationClassPostProcessorTests {
public static class Z {
}
@Configuration
static class BeanArgumentConfig {
@Bean
@Scope("prototype")
public DependingFoo foo(final BarArgument bar) {
return new DependingFoo(bar);
}
@Bean
public FooFactory fooFactory() {
return new FooFactory() {
@Override
public DependingFoo createFoo(final BarArgument bar) {
return foo(bar);
}
};
}
}
static class BarArgument {
}
static class DependingFoo {
DependingFoo(BarArgument bar) {
}
}
static abstract class FooFactory {
abstract DependingFoo createFoo(BarArgument bar);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@ -34,47 +34,55 @@ import static org.junit.Assert.*;
* @since 3.1
*/
public class ConfigurationWithFactoryBeanAndParametersTests {
@Test
public void test() {
ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class, Bar.class);
assertNotNull(ctx.getBean(Bar.class).foo);
}
}
@Configuration
class Config {
@Bean
public FactoryBean<Foo> fb(@Value("42") String answer) {
return new FooFactoryBean();
}
}
class Foo {
}
@Configuration
static class Config {
class Bar {
Foo foo;
@Autowired
public Bar(Foo foo) {
this.foo = foo;
}
}
class FooFactoryBean implements FactoryBean<Foo> {
@Override
public Foo getObject() {
return new Foo();
@Bean
public FactoryBean<Foo> fb(@Value("42") String answer) {
return new FooFactoryBean();
}
}
@Override
public Class<Foo> getObjectType() {
return Foo.class;
static class Foo {
}
@Override
public boolean isSingleton() {
return true;
static class Bar {
Foo foo;
@Autowired
public Bar(Foo foo) {
this.foo = foo;
}
}
static class FooFactoryBean implements FactoryBean<Foo> {
@Override
public Foo getObject() {
return new Foo();
}
@Override
public Class<Foo> getObjectType() {
return Foo.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
}