From 4bd75e4146ace622b9224b661766289148996ed2 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 22 Nov 2014 16:06:25 +0100 Subject: [PATCH] BeanMethodInterceptor forwards user-provided arguments to getBean(name, args) Issue: SPR-12443 --- .../ConfigurationClassEnhancer.java | 4 +- .../ConfigurationClassPostProcessorTests.java | 40 +++++++++++ ...tionWithFactoryBeanAndParametersTests.java | 70 +++++++++++-------- 3 files changed, 82 insertions(+), 32 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java index f7255b011ac..5574a05ea66 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java @@ -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) { diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java index 421e7382143..0c1ccf2d7db 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java @@ -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); + } + } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationWithFactoryBeanAndParametersTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationWithFactoryBeanAndParametersTests.java index 89ef4505383..20a6f82aef3 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationWithFactoryBeanAndParametersTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationWithFactoryBeanAndParametersTests.java @@ -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 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 { - - @Override - public Foo getObject() { - return new Foo(); + @Bean + public FactoryBean fb(@Value("42") String answer) { + return new FooFactoryBean(); + } } - @Override - public Class 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 { + + @Override + public Foo getObject() { + return new Foo(); + } + + @Override + public Class getObjectType() { + return Foo.class; + } + + @Override + public boolean isSingleton() { + return true; + } + } + }