1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -1343,6 +1343,16 @@ public void testGetBeanByTypeWithNoneFound() {
1343
1343
lbf .getBean (TestBean .class );
1344
1344
}
1345
1345
1346
+ @ Test
1347
+ public void testGetBeanByTypeDefinedInParent () {
1348
+ DefaultListableBeanFactory parent = new DefaultListableBeanFactory ();
1349
+ RootBeanDefinition bd1 = new RootBeanDefinition (TestBean .class );
1350
+ parent .registerBeanDefinition ("bd1" , bd1 );
1351
+ DefaultListableBeanFactory lbf = new DefaultListableBeanFactory (parent );
1352
+ TestBean bean = lbf .getBean (TestBean .class );
1353
+ assertThat (bean .getBeanName (), equalTo ("bd1" ));
1354
+ }
1355
+
1346
1356
@ Test (expected =NoUniqueBeanDefinitionException .class )
1347
1357
public void testGetBeanByTypeWithAmbiguity () {
1348
1358
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory ();
@@ -1449,6 +1459,95 @@ public void testGetBeanByTypeFiltersOutNonAutowireCandidates() {
1449
1459
}
1450
1460
}
1451
1461
1462
+ @ Test (expected = NoSuchBeanDefinitionException .class )
1463
+ public void testGetBeanByTypeInstanceWithNoneFound () {
1464
+ DefaultListableBeanFactory lbf = new DefaultListableBeanFactory ();
1465
+ lbf .getBean (ConstructorDependency .class , 42 );
1466
+ }
1467
+
1468
+ @ Test
1469
+ public void testGetBeanByTypeInstanceDefinedInParent () {
1470
+ DefaultListableBeanFactory parent = new DefaultListableBeanFactory ();
1471
+ RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition (99 );
1472
+ parent .registerBeanDefinition ("bd1" , bd1 );
1473
+ DefaultListableBeanFactory lbf = new DefaultListableBeanFactory (parent );
1474
+ ConstructorDependency bean = lbf .getBean (ConstructorDependency .class , 42 );
1475
+ assertThat (bean .beanName , equalTo ("bd1" ));
1476
+ assertThat (bean .spouseAge , equalTo (42 ));
1477
+ }
1478
+
1479
+ @ Test
1480
+ public void testGetBeanByTypeInstanceWithAmbiguity () {
1481
+ DefaultListableBeanFactory lbf = new DefaultListableBeanFactory ();
1482
+ RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition (99 );
1483
+ RootBeanDefinition bd2 = new RootBeanDefinition (ConstructorDependency .class );
1484
+ bd2 .setScope (RootBeanDefinition .SCOPE_PROTOTYPE );
1485
+ bd2 .getConstructorArgumentValues ().addGenericArgumentValue ("43" );
1486
+
1487
+ lbf .registerBeanDefinition ("bd1" , bd1 );
1488
+ lbf .registerBeanDefinition ("bd2" , bd2 );
1489
+
1490
+ thrown .expect (NoUniqueBeanDefinitionException .class );
1491
+ lbf .getBean (ConstructorDependency .class , 42 );
1492
+ }
1493
+
1494
+ @ Test
1495
+ public void testGetBeanByTypeInstanceWithPrimary () throws Exception {
1496
+ DefaultListableBeanFactory lbf = new DefaultListableBeanFactory ();
1497
+ RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition (99 );
1498
+ RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition (43 );
1499
+ bd2 .setPrimary (true );
1500
+ lbf .registerBeanDefinition ("bd1" , bd1 );
1501
+ lbf .registerBeanDefinition ("bd2" , bd2 );
1502
+ ConstructorDependency bean = lbf .getBean (ConstructorDependency .class , 42 );
1503
+ assertThat (bean .beanName , equalTo ("bd2" ));
1504
+ assertThat (bean .spouseAge , equalTo (42 ));
1505
+ }
1506
+
1507
+ @ Test
1508
+ public void testGetBeanByTypeInstanceWithMultiplePrimary () throws Exception {
1509
+ DefaultListableBeanFactory lbf = new DefaultListableBeanFactory ();
1510
+ RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition (99 );
1511
+ RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition (43 );
1512
+ bd1 .setPrimary (true );
1513
+ bd2 .setPrimary (true );
1514
+
1515
+ lbf .registerBeanDefinition ("bd1" , bd1 );
1516
+ lbf .registerBeanDefinition ("bd2" , bd2 );
1517
+ thrown .expect (NoUniqueBeanDefinitionException .class );
1518
+ thrown .expectMessage (containsString ("more than one 'primary'" ));
1519
+ lbf .getBean (ConstructorDependency .class , 42 );
1520
+ }
1521
+
1522
+ @ Test
1523
+ public void testGetBeanByTypeInstanceFiltersOutNonAutowireCandidates () {
1524
+ DefaultListableBeanFactory lbf = new DefaultListableBeanFactory ();
1525
+ RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition (99 );
1526
+ RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition (43 );
1527
+ RootBeanDefinition na1 = createConstructorDependencyBeanDefinition (21 );
1528
+ na1 .setAutowireCandidate (false );
1529
+
1530
+ lbf .registerBeanDefinition ("bd1" , bd1 );
1531
+ lbf .registerBeanDefinition ("na1" , na1 );
1532
+ ConstructorDependency actual = lbf .getBean (ConstructorDependency .class , 42 ); // na1 was filtered
1533
+ assertThat (actual .beanName , equalTo ("bd1" ));
1534
+
1535
+ lbf .registerBeanDefinition ("bd2" , bd2 );
1536
+ try {
1537
+ lbf .getBean (TestBean .class , 67 );
1538
+ fail ("Should have thrown NoSuchBeanDefinitionException" );
1539
+ } catch (NoSuchBeanDefinitionException ex ) {
1540
+ // expected
1541
+ }
1542
+ }
1543
+
1544
+ private RootBeanDefinition createConstructorDependencyBeanDefinition (int age ) {
1545
+ RootBeanDefinition bd1 = new RootBeanDefinition (ConstructorDependency .class );
1546
+ bd1 .setScope (RootBeanDefinition .SCOPE_PROTOTYPE );
1547
+ bd1 .getConstructorArgumentValues ().addGenericArgumentValue (String .valueOf (age ));
1548
+ return bd1 ;
1549
+ }
1550
+
1452
1551
@ Test
1453
1552
public void testAutowireBeanByType () {
1454
1553
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory ();
@@ -2525,18 +2624,31 @@ private NoDependencies() {
2525
2624
}
2526
2625
2527
2626
2528
- public static class ConstructorDependency {
2627
+ public static class ConstructorDependency implements BeanNameAware {
2529
2628
2530
2629
public TestBean spouse ;
2531
2630
2631
+ public int spouseAge ;
2632
+
2633
+ private String beanName ;
2634
+
2532
2635
public ConstructorDependency (TestBean spouse ) {
2533
2636
this .spouse = spouse ;
2534
2637
}
2535
2638
2639
+ public ConstructorDependency (int spouseAge ) {
2640
+ this .spouseAge = spouseAge ;
2641
+ }
2642
+
2536
2643
@ SuppressWarnings ("unused" )
2537
2644
private ConstructorDependency (TestBean spouse , TestBean otherSpouse ) {
2538
2645
throw new IllegalArgumentException ("Should never be called" );
2539
2646
}
2647
+
2648
+ @ Override
2649
+ public void setBeanName (String name ) {
2650
+ this .beanName = name ;
2651
+ }
2540
2652
}
2541
2653
2542
2654
@@ -2879,6 +2991,7 @@ public String getUserName() {
2879
2991
}
2880
2992
}
2881
2993
2994
+
2882
2995
@ SuppressWarnings ("unused" )
2883
2996
private static class KnowsIfInstantiated {
2884
2997
@@ -2898,11 +3011,16 @@ public KnowsIfInstantiated() {
2898
3011
2899
3012
}
2900
3013
3014
+
2901
3015
@ Priority (5 )
2902
- private static class HighPriorityTestBean extends TestBean {}
3016
+ private static class HighPriorityTestBean extends TestBean {
3017
+ }
3018
+
2903
3019
2904
3020
@ Priority (500 )
2905
- private static class LowPriorityTestBean extends TestBean {}
3021
+ private static class LowPriorityTestBean extends TestBean {
3022
+ }
3023
+
2906
3024
2907
3025
private static class NullTestBeanFactoryBean <T > implements FactoryBean <TestBean > {
2908
3026
@@ -2922,5 +3040,4 @@ public boolean isSingleton() {
2922
3040
}
2923
3041
}
2924
3042
2925
-
2926
3043
}
0 commit comments