|
25 | 25 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
26 | 26 | import org.springframework.beans.factory.annotation.Autowired;
|
27 | 27 | import org.springframework.beans.factory.config.BeanPostProcessor;
|
| 28 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
28 | 29 | import org.springframework.context.ApplicationContext;
|
29 | 30 | import org.springframework.context.annotation6.ComponentForScanning;
|
30 | 31 | import org.springframework.context.annotation6.ConfigForScanning;
|
31 | 32 | import org.springframework.context.annotation6.Jsr330NamedForScanning;
|
| 33 | +import org.springframework.core.ResolvableType; |
32 | 34 | import org.springframework.util.ObjectUtils;
|
33 | 35 |
|
34 | 36 | import static java.lang.String.*;
|
@@ -279,6 +281,30 @@ public void individualNamedBeanWithMixedConstructorArguments() {
|
279 | 281 | assertSame(context, context.getBean("b", BeanB.class).applicationContext);
|
280 | 282 | }
|
281 | 283 |
|
| 284 | + @Test |
| 285 | + public void individualBeanWithFactoryBeanSupplier() { |
| 286 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 287 | + context.registerBean("fb", TypedFactoryBean.class, TypedFactoryBean::new, bd -> bd.setLazyInit(true)); |
| 288 | + context.refresh(); |
| 289 | + |
| 290 | + assertEquals(String.class, context.getType("fb")); |
| 291 | + assertEquals(TypedFactoryBean.class, context.getType("&fb")); |
| 292 | + } |
| 293 | + |
| 294 | + @Test |
| 295 | + public void individualBeanWithFactoryBeanSupplierAndTargetType() { |
| 296 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 297 | + RootBeanDefinition bd = new RootBeanDefinition(); |
| 298 | + bd.setInstanceSupplier(TypedFactoryBean::new); |
| 299 | + bd.setTargetType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)); |
| 300 | + bd.setLazyInit(true); |
| 301 | + context.registerBeanDefinition("fb", bd); |
| 302 | + context.refresh(); |
| 303 | + |
| 304 | + assertEquals(String.class, context.getType("fb")); |
| 305 | + assertEquals(FactoryBean.class, context.getType("&fb")); |
| 306 | + } |
| 307 | + |
282 | 308 | @Test
|
283 | 309 | public void getBeanByTypeRaisesNoSuchBeanDefinitionException() {
|
284 | 310 | ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
|
@@ -428,6 +454,28 @@ public BeanB() {
|
428 | 454 |
|
429 | 455 | static class BeanC {}
|
430 | 456 |
|
| 457 | + static class TypedFactoryBean implements FactoryBean<String> { |
| 458 | + |
| 459 | + public TypedFactoryBean() { |
| 460 | + throw new IllegalStateException(); |
| 461 | + } |
| 462 | + |
| 463 | + @Override |
| 464 | + public String getObject() { |
| 465 | + return ""; |
| 466 | + } |
| 467 | + |
| 468 | + @Override |
| 469 | + public Class<?> getObjectType() { |
| 470 | + return String.class; |
| 471 | + } |
| 472 | + |
| 473 | + @Override |
| 474 | + public boolean isSingleton() { |
| 475 | + return true; |
| 476 | + } |
| 477 | + } |
| 478 | + |
431 | 479 | static class UntypedFactoryBean implements FactoryBean<Object> {
|
432 | 480 |
|
433 | 481 | @Override
|
|
0 commit comments