65
65
import org .springframework .beans .factory .support .BeanDefinitionOverrideException ;
66
66
import org .springframework .beans .factory .support .ChildBeanDefinition ;
67
67
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
68
+ import org .springframework .beans .factory .support .GenericBeanDefinition ;
68
69
import org .springframework .beans .factory .support .ManagedList ;
69
70
import org .springframework .beans .factory .support .RootBeanDefinition ;
70
71
import org .springframework .beans .factory .xml .ConstructorDependenciesBean ;
@@ -1370,10 +1371,10 @@ void autowireWithTwoMatchesForConstructorDependency() {
1370
1371
lbf .registerBeanDefinition ("rod2" , bd2 );
1371
1372
lbf .setParameterNameDiscoverer (new DefaultParameterNameDiscoverer ());
1372
1373
1373
- assertThatExceptionOfType (UnsatisfiedDependencyException .class ). isThrownBy (() ->
1374
- lbf .autowire (ConstructorDependency .class , AutowireCapableBeanFactory .AUTOWIRE_CONSTRUCTOR , false ))
1375
- .withMessageContaining ("rod" )
1376
- .withMessageContaining ("rod2" );
1374
+ assertThatExceptionOfType (UnsatisfiedDependencyException .class )
1375
+ . isThrownBy (() -> lbf .autowire (ConstructorDependency .class , AutowireCapableBeanFactory .AUTOWIRE_CONSTRUCTOR , false ))
1376
+ .withMessageContaining ("rod" )
1377
+ .withMessageContaining ("rod2" );
1377
1378
}
1378
1379
1379
1380
@ Test
@@ -1432,6 +1433,57 @@ void autowireBeanByNameWithNoDependencyCheck() {
1432
1433
assertThat (bean .getSpouse ()).isNull ();
1433
1434
}
1434
1435
1436
+ @ Test
1437
+ void autowirePreferredConstructors () {
1438
+ lbf .registerBeanDefinition ("spouse1" , new RootBeanDefinition (TestBean .class ));
1439
+ lbf .registerBeanDefinition ("spouse2" , new RootBeanDefinition (TestBean .class ));
1440
+ RootBeanDefinition bd = new RootBeanDefinition (ConstructorDependenciesBean .class );
1441
+ bd .setAutowireMode (RootBeanDefinition .AUTOWIRE_CONSTRUCTOR );
1442
+ lbf .registerBeanDefinition ("bean" , bd );
1443
+ lbf .setParameterNameDiscoverer (new DefaultParameterNameDiscoverer ());
1444
+
1445
+ ConstructorDependenciesBean bean = lbf .getBean (ConstructorDependenciesBean .class );
1446
+ Object spouse1 = lbf .getBean ("spouse1" );
1447
+ Object spouse2 = lbf .getBean ("spouse2" );
1448
+ assertThat (bean .getSpouse1 ()).isSameAs (spouse1 );
1449
+ assertThat (bean .getSpouse2 ()).isSameAs (spouse2 );
1450
+ }
1451
+
1452
+ @ Test
1453
+ void autowirePreferredConstructorsFromAttribute () {
1454
+ lbf .registerBeanDefinition ("spouse1" , new RootBeanDefinition (TestBean .class ));
1455
+ lbf .registerBeanDefinition ("spouse2" , new RootBeanDefinition (TestBean .class ));
1456
+ GenericBeanDefinition bd = new GenericBeanDefinition ();
1457
+ bd .setBeanClass (ConstructorDependenciesBean .class );
1458
+ bd .setAttribute (GenericBeanDefinition .PREFERRED_CONSTRUCTORS_ATTRIBUTE ,
1459
+ ConstructorDependenciesBean .class .getConstructors ());
1460
+ lbf .registerBeanDefinition ("bean" , bd );
1461
+ lbf .setParameterNameDiscoverer (new DefaultParameterNameDiscoverer ());
1462
+
1463
+ ConstructorDependenciesBean bean = lbf .getBean (ConstructorDependenciesBean .class );
1464
+ Object spouse1 = lbf .getBean ("spouse1" );
1465
+ Object spouse2 = lbf .getBean ("spouse2" );
1466
+ assertThat (bean .getSpouse1 ()).isSameAs (spouse1 );
1467
+ assertThat (bean .getSpouse2 ()).isSameAs (spouse2 );
1468
+ }
1469
+
1470
+ @ Test
1471
+ void autowirePreferredConstructorFromAttribute () throws Exception {
1472
+ lbf .registerBeanDefinition ("spouse1" , new RootBeanDefinition (TestBean .class ));
1473
+ lbf .registerBeanDefinition ("spouse2" , new RootBeanDefinition (TestBean .class ));
1474
+ GenericBeanDefinition bd = new GenericBeanDefinition ();
1475
+ bd .setBeanClass (ConstructorDependenciesBean .class );
1476
+ bd .setAttribute (GenericBeanDefinition .PREFERRED_CONSTRUCTORS_ATTRIBUTE ,
1477
+ ConstructorDependenciesBean .class .getConstructor (TestBean .class ));
1478
+ lbf .registerBeanDefinition ("bean" , bd );
1479
+ lbf .setParameterNameDiscoverer (new DefaultParameterNameDiscoverer ());
1480
+
1481
+ ConstructorDependenciesBean bean = lbf .getBean (ConstructorDependenciesBean .class );
1482
+ Object spouse = lbf .getBean ("spouse1" );
1483
+ assertThat (bean .getSpouse1 ()).isSameAs (spouse );
1484
+ assertThat (bean .getSpouse2 ()).isNull ();
1485
+ }
1486
+
1435
1487
@ Test
1436
1488
void dependsOnCycle () {
1437
1489
RootBeanDefinition bd1 = new RootBeanDefinition (TestBean .class );
@@ -1441,11 +1493,11 @@ void dependsOnCycle() {
1441
1493
bd2 .setDependsOn ("tb1" );
1442
1494
lbf .registerBeanDefinition ("tb2" , bd2 );
1443
1495
1444
- assertThatExceptionOfType (BeanCreationException .class ). isThrownBy (() ->
1445
- lbf .preInstantiateSingletons ())
1446
- .withMessageContaining ("Circular" )
1447
- .withMessageContaining ("'tb2'" )
1448
- .withMessageContaining ("'tb1'" );
1496
+ assertThatExceptionOfType (BeanCreationException .class )
1497
+ . isThrownBy (() -> lbf .preInstantiateSingletons ())
1498
+ .withMessageContaining ("Circular" )
1499
+ .withMessageContaining ("'tb2'" )
1500
+ .withMessageContaining ("'tb1'" );
1449
1501
}
1450
1502
1451
1503
@ Test
@@ -1460,11 +1512,11 @@ void implicitDependsOnCycle() {
1460
1512
bd3 .setDependsOn ("tb1" );
1461
1513
lbf .registerBeanDefinition ("tb3" , bd3 );
1462
1514
1463
- assertThatExceptionOfType (BeanCreationException .class ). isThrownBy (
1464
- lbf ::preInstantiateSingletons )
1465
- .withMessageContaining ("Circular" )
1466
- .withMessageContaining ("'tb3'" )
1467
- .withMessageContaining ("'tb1'" );
1515
+ assertThatExceptionOfType (BeanCreationException .class )
1516
+ . isThrownBy ( lbf ::preInstantiateSingletons )
1517
+ .withMessageContaining ("Circular" )
1518
+ .withMessageContaining ("'tb3'" )
1519
+ .withMessageContaining ("'tb1'" );
1468
1520
}
1469
1521
1470
1522
@ Test
@@ -1562,9 +1614,9 @@ void getBeanByTypeWithMultiplePrimary() {
1562
1614
lbf .registerBeanDefinition ("bd1" , bd1 );
1563
1615
lbf .registerBeanDefinition ("bd2" , bd2 );
1564
1616
1565
- assertThatExceptionOfType (NoUniqueBeanDefinitionException .class ). isThrownBy (() ->
1566
- lbf .getBean (TestBean .class ))
1567
- .withMessageContaining ("more than one 'primary'" );
1617
+ assertThatExceptionOfType (NoUniqueBeanDefinitionException .class )
1618
+ . isThrownBy (() -> lbf .getBean (TestBean .class ))
1619
+ .withMessageContaining ("more than one 'primary'" );
1568
1620
}
1569
1621
1570
1622
@ Test
@@ -1607,10 +1659,10 @@ void getBeanByTypeWithMultiplePriority() {
1607
1659
lbf .registerBeanDefinition ("bd1" , bd1 );
1608
1660
lbf .registerBeanDefinition ("bd2" , bd2 );
1609
1661
1610
- assertThatExceptionOfType (NoUniqueBeanDefinitionException .class ). isThrownBy (() ->
1611
- lbf .getBean (TestBean .class ))
1612
- .withMessageContaining ("Multiple beans found with the same priority" )
1613
- .withMessageContaining ("5" ); // conflicting priority
1662
+ assertThatExceptionOfType (NoUniqueBeanDefinitionException .class )
1663
+ . isThrownBy (() -> lbf .getBean (TestBean .class ))
1664
+ .withMessageContaining ("Multiple beans found with the same priority" )
1665
+ .withMessageContaining ("5" ); // conflicting priority
1614
1666
}
1615
1667
1616
1668
@ Test
@@ -1815,9 +1867,9 @@ void getBeanByTypeInstanceWithMultiplePrimary() {
1815
1867
lbf .registerBeanDefinition ("bd1" , bd1 );
1816
1868
lbf .registerBeanDefinition ("bd2" , bd2 );
1817
1869
1818
- assertThatExceptionOfType (NoUniqueBeanDefinitionException .class ). isThrownBy (() ->
1819
- lbf .getBean (ConstructorDependency .class , 42 ))
1820
- .withMessageContaining ("more than one 'primary'" );
1870
+ assertThatExceptionOfType (NoUniqueBeanDefinitionException .class )
1871
+ . isThrownBy (() -> lbf .getBean (ConstructorDependency .class , 42 ))
1872
+ .withMessageContaining ("more than one 'primary'" );
1821
1873
}
1822
1874
1823
1875
@ Test
@@ -2004,10 +2056,10 @@ void autowireBeanByTypeWithTwoMatches() {
2004
2056
lbf .registerBeanDefinition ("test" , bd );
2005
2057
lbf .registerBeanDefinition ("spouse" , bd2 );
2006
2058
2007
- assertThatExceptionOfType (UnsatisfiedDependencyException .class ). isThrownBy (() ->
2008
- lbf .autowire (DependenciesBean .class , AutowireCapableBeanFactory .AUTOWIRE_BY_TYPE , true ))
2009
- .withMessageContaining ("test" )
2010
- .withMessageContaining ("spouse" );
2059
+ assertThatExceptionOfType (UnsatisfiedDependencyException .class )
2060
+ . isThrownBy (() -> lbf .autowire (DependenciesBean .class , AutowireCapableBeanFactory .AUTOWIRE_BY_TYPE , true ))
2061
+ .withMessageContaining ("test" )
2062
+ .withMessageContaining ("spouse" );
2011
2063
}
2012
2064
2013
2065
@ Test
@@ -2071,10 +2123,10 @@ void autowireBeanByTypeWithIdenticalPriorityCandidates() {
2071
2123
lbf .registerBeanDefinition ("test" , bd );
2072
2124
lbf .registerBeanDefinition ("spouse" , bd2 );
2073
2125
2074
- assertThatExceptionOfType (UnsatisfiedDependencyException .class ). isThrownBy (() ->
2075
- lbf .autowire (DependenciesBean .class , AutowireCapableBeanFactory .AUTOWIRE_BY_TYPE , true ))
2076
- .withCauseExactlyInstanceOf (NoUniqueBeanDefinitionException .class )
2077
- .withMessageContaining ("5" );
2126
+ assertThatExceptionOfType (UnsatisfiedDependencyException .class )
2127
+ . isThrownBy (() -> lbf .autowire (DependenciesBean .class , AutowireCapableBeanFactory .AUTOWIRE_BY_TYPE , true ))
2128
+ .withCauseExactlyInstanceOf (NoUniqueBeanDefinitionException .class )
2129
+ .withMessageContaining ("5" );
2078
2130
}
2079
2131
2080
2132
@ Test
@@ -2337,20 +2389,20 @@ void constructorDependencyWithUnresolvableClass() {
2337
2389
void beanDefinitionWithInterface () {
2338
2390
lbf .registerBeanDefinition ("test" , new RootBeanDefinition (ITestBean .class ));
2339
2391
2340
- assertThatExceptionOfType (BeanCreationException .class ). isThrownBy (() ->
2341
- lbf .getBean ("test" ))
2342
- .withMessageContaining ("interface" )
2343
- .satisfies (ex -> assertThat (ex .getBeanName ()).isEqualTo ("test" ));
2392
+ assertThatExceptionOfType (BeanCreationException .class )
2393
+ . isThrownBy (() -> lbf .getBean ("test" ))
2394
+ .withMessageContaining ("interface" )
2395
+ .satisfies (ex -> assertThat (ex .getBeanName ()).isEqualTo ("test" ));
2344
2396
}
2345
2397
2346
2398
@ Test
2347
2399
void beanDefinitionWithAbstractClass () {
2348
2400
lbf .registerBeanDefinition ("test" , new RootBeanDefinition (AbstractBeanFactory .class ));
2349
2401
2350
- assertThatExceptionOfType (BeanCreationException .class ). isThrownBy (() ->
2351
- lbf .getBean ("test" ))
2352
- .withMessageContaining ("abstract" )
2353
- .satisfies (ex -> assertThat (ex .getBeanName ()).isEqualTo ("test" ));
2402
+ assertThatExceptionOfType (BeanCreationException .class )
2403
+ . isThrownBy (() -> lbf .getBean ("test" ))
2404
+ .withMessageContaining ("abstract" )
2405
+ .satisfies (ex -> assertThat (ex .getBeanName ()).isEqualTo ("test" ));
2354
2406
}
2355
2407
2356
2408
@ Test
0 commit comments