Skip to content

Commit 4bd75e4

Browse files
committed
BeanMethodInterceptor forwards user-provided arguments to getBean(name, args)
Issue: SPR-12443
1 parent 75c70fa commit 4bd75e4

File tree

3 files changed

+79
-29
lines changed

3 files changed

+79
-29
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.cglib.transform.TransformingClassGenerator;
4646
import org.springframework.core.annotation.AnnotationUtils;
4747
import org.springframework.util.Assert;
48+
import org.springframework.util.ObjectUtils;
4849
import org.springframework.util.ReflectionUtils;
4950

5051
/**
@@ -321,7 +322,8 @@ public Object intercept(Object enhancedConfigInstance, Method beanMethod, Object
321322
if (alreadyInCreation) {
322323
beanFactory.setCurrentlyInCreation(beanName, false);
323324
}
324-
return beanFactory.getBean(beanName);
325+
return (!ObjectUtils.isEmpty(beanMethodArgs) ?
326+
beanFactory.getBean(beanName, beanMethodArgs) : beanFactory.getBean(beanName));
325327
}
326328
finally {
327329
if (alreadyInCreation) {

spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,12 @@ public void testCircularDependencyWithApplicationContext() {
468468
}
469469
}
470470

471+
@Test
472+
public void testPrototypeArgumentsThroughBeanMethodCall() {
473+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(BeanArgumentConfig.class);
474+
ctx.getBean(FooFactory.class).createFoo(new BarArgument());
475+
}
476+
471477

472478
// -------------------------------------------------------------------------
473479

@@ -925,4 +931,38 @@ public B(Z z) {
925931
public static class Z {
926932
}
927933

934+
@Configuration
935+
static class BeanArgumentConfig {
936+
937+
@Bean
938+
@Scope("prototype")
939+
public DependingFoo foo(final BarArgument bar) {
940+
return new DependingFoo(bar);
941+
}
942+
943+
@Bean
944+
public FooFactory fooFactory() {
945+
return new FooFactory() {
946+
@Override
947+
public DependingFoo createFoo(final BarArgument bar) {
948+
return foo(bar);
949+
}
950+
};
951+
}
952+
}
953+
954+
static class BarArgument {
955+
}
956+
957+
static class DependingFoo {
958+
959+
DependingFoo(BarArgument bar) {
960+
}
961+
}
962+
963+
static abstract class FooFactory {
964+
965+
abstract DependingFoo createFoo(BarArgument bar);
966+
}
967+
928968
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,47 +34,55 @@
3434
* @since 3.1
3535
*/
3636
public class ConfigurationWithFactoryBeanAndParametersTests {
37+
3738
@Test
3839
public void test() {
3940
ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class, Bar.class);
4041
assertNotNull(ctx.getBean(Bar.class).foo);
4142
}
42-
}
43-
44-
@Configuration
45-
class Config {
46-
@Bean
47-
public FactoryBean<Foo> fb(@Value("42") String answer) {
48-
return new FooFactoryBean();
49-
}
50-
}
5143

52-
class Foo {
53-
}
5444

55-
class Bar {
56-
Foo foo;
45+
@Configuration
46+
static class Config {
5747

58-
@Autowired
59-
public Bar(Foo foo) {
60-
this.foo = foo;
48+
@Bean
49+
public FactoryBean<Foo> fb(@Value("42") String answer) {
50+
return new FooFactoryBean();
51+
}
6152
}
62-
}
6353

64-
class FooFactoryBean implements FactoryBean<Foo> {
6554

66-
@Override
67-
public Foo getObject() {
68-
return new Foo();
55+
static class Foo {
6956
}
7057

71-
@Override
72-
public Class<Foo> getObjectType() {
73-
return Foo.class;
58+
59+
static class Bar {
60+
61+
Foo foo;
62+
63+
@Autowired
64+
public Bar(Foo foo) {
65+
this.foo = foo;
66+
}
7467
}
7568

76-
@Override
77-
public boolean isSingleton() {
78-
return true;
69+
70+
static class FooFactoryBean implements FactoryBean<Foo> {
71+
72+
@Override
73+
public Foo getObject() {
74+
return new Foo();
75+
}
76+
77+
@Override
78+
public Class<Foo> getObjectType() {
79+
return Foo.class;
80+
}
81+
82+
@Override
83+
public boolean isSingleton() {
84+
return true;
85+
}
7986
}
87+
8088
}

0 commit comments

Comments
 (0)