Skip to content

Commit f42e886

Browse files
committed
Polishing
1 parent de4db38 commit f42e886

File tree

2 files changed

+73
-50
lines changed

2 files changed

+73
-50
lines changed

Diff for: spring-context/src/test/java/org/springframework/context/annotation/BeanMethodPolymorphismTests.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -242,7 +242,8 @@ static class Config extends BaseConfig {
242242
@Configuration
243243
static class OverridingConfig extends BaseConfig {
244244

245-
@Bean @Lazy
245+
@Bean
246+
@Lazy
246247
@Override
247248
public BaseTestBean testBean() {
248249
return new BaseTestBean() {
@@ -258,7 +259,8 @@ public String toString() {
258259
@Configuration
259260
static class OverridingConfigWithDifferentBeanName extends BaseConfig {
260261

261-
@Bean("myTestBean") @Lazy
262+
@Bean("myTestBean")
263+
@Lazy
262264
@Override
263265
public BaseTestBean testBean() {
264266
return new BaseTestBean() {
@@ -274,7 +276,8 @@ public String toString() {
274276
@Configuration
275277
static class NarrowedOverridingConfig extends BaseConfig {
276278

277-
@Bean @Lazy
279+
@Bean
280+
@Lazy
278281
@Override
279282
public ExtendedTestBean testBean() {
280283
return new ExtendedTestBean() {
@@ -287,6 +290,7 @@ public String toString() {
287290
}
288291

289292

293+
@SuppressWarnings("deprecation")
290294
@Configuration(enforceUniqueMethods = false)
291295
static class ConfigWithOverloading {
292296

@@ -302,15 +306,18 @@ String aString(Integer dependency) {
302306
}
303307

304308

309+
@SuppressWarnings("deprecation")
305310
@Configuration(enforceUniqueMethods = false)
306311
static class ConfigWithOverloadingAndAdditionalMetadata {
307312

308-
@Bean @Lazy
313+
@Bean
314+
@Lazy
309315
String aString() {
310316
return "regular";
311317
}
312318

313-
@Bean @Lazy
319+
@Bean
320+
@Lazy
314321
String aString(Integer dependency) {
315322
return "overloaded" + dependency;
316323
}
@@ -335,7 +342,8 @@ Integer anInt() {
335342
return 5;
336343
}
337344

338-
@Bean @Lazy
345+
@Bean
346+
@Lazy
339347
String aString(Integer dependency) {
340348
return "overloaded" + dependency;
341349
}
@@ -350,7 +358,8 @@ Integer anInt() {
350358
return 5;
351359
}
352360

353-
@Bean @Lazy
361+
@Bean
362+
@Lazy
354363
String aString(List<Integer> dependency) {
355364
return "overloaded" + dependency.get(0);
356365
}

Diff for: spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java

+56-42
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -83,7 +83,7 @@ void customBeanNameIsRespectedWhenConfiguredViaValueAttribute() {
8383
() -> ConfigWithBeanWithCustomNameConfiguredViaValueAttribute.testBean, "enigma");
8484
}
8585

86-
private void customBeanNameIsRespected(Class<?> testClass, Supplier<TestBean> testBeanSupplier, String beanName) {
86+
private static void customBeanNameIsRespected(Class<?> testClass, Supplier<TestBean> testBeanSupplier, String beanName) {
8787
GenericApplicationContext ac = new GenericApplicationContext();
8888
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
8989
ac.registerBeanDefinition("config", new RootBeanDefinition(testClass));
@@ -92,8 +92,8 @@ private void customBeanNameIsRespected(Class<?> testClass, Supplier<TestBean> te
9292
assertThat(ac.getBean(beanName)).isSameAs(testBeanSupplier.get());
9393

9494
// method name should not be registered
95-
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() ->
96-
ac.getBean("methodName"));
95+
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
96+
.isThrownBy(() -> ac.getBean("methodName"));
9797
}
9898

9999
@Test
@@ -113,11 +113,12 @@ private void aliasesAreRespected(Class<?> testClass, Supplier<TestBean> testBean
113113
BeanFactory factory = initBeanFactory(false, testClass);
114114

115115
assertThat(factory.getBean(beanName)).isSameAs(testBean);
116-
Arrays.stream(factory.getAliases(beanName)).map(factory::getBean).forEach(alias -> assertThat(alias).isSameAs(testBean));
116+
assertThat(factory.getAliases(beanName)).extracting(factory::getBean)
117+
.allMatch(alias -> alias == testBean);
117118

118119
// method name should not be registered
119-
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() ->
120-
factory.getBean("methodName"));
120+
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
121+
.isThrownBy(() -> factory.getBean("methodName"));
121122
}
122123

123124
@Test // SPR-11830
@@ -140,8 +141,8 @@ void configWithSetWithProviderImplementation() {
140141

141142
@Test
142143
void finalBeanMethod() {
143-
assertThatExceptionOfType(BeanDefinitionParsingException.class).isThrownBy(() ->
144-
initBeanFactory(false, ConfigWithFinalBean.class));
144+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
145+
.isThrownBy(() -> initBeanFactory(false, ConfigWithFinalBean.class));
145146
}
146147

147148
@Test
@@ -151,8 +152,8 @@ void finalBeanMethodWithoutProxy() {
151152

152153
@Test // gh-31007
153154
void voidBeanMethod() {
154-
assertThatExceptionOfType(BeanDefinitionParsingException.class).isThrownBy(() ->
155-
initBeanFactory(false, ConfigWithVoidBean.class));
155+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
156+
.isThrownBy(() -> initBeanFactory(false, ConfigWithVoidBean.class));
156157
}
157158

158159
@Test
@@ -180,23 +181,19 @@ void configWithFactoryBeanReturnType() {
180181
assertThat(factory.isTypeMatch("&factoryBean", FactoryBean.class)).isTrue();
181182
assertThat(factory.isTypeMatch("&factoryBean", BeanClassLoaderAware.class)).isFalse();
182183
assertThat(factory.isTypeMatch("&factoryBean", ListFactoryBean.class)).isFalse();
183-
boolean condition = factory.getBean("factoryBean") instanceof List;
184-
assertThat(condition).isTrue();
184+
assertThat(factory.getBean("factoryBean")).isInstanceOf(List.class);
185185

186186
String[] beanNames = factory.getBeanNamesForType(FactoryBean.class);
187-
assertThat(beanNames).hasSize(1);
188-
assertThat(beanNames[0]).isEqualTo("&factoryBean");
187+
assertThat(beanNames).containsExactly("&factoryBean");
189188

190189
beanNames = factory.getBeanNamesForType(BeanClassLoaderAware.class);
191-
assertThat(beanNames).hasSize(1);
192-
assertThat(beanNames[0]).isEqualTo("&factoryBean");
190+
assertThat(beanNames).containsExactly("&factoryBean");
193191

194192
beanNames = factory.getBeanNamesForType(ListFactoryBean.class);
195-
assertThat(beanNames).hasSize(1);
196-
assertThat(beanNames[0]).isEqualTo("&factoryBean");
193+
assertThat(beanNames).containsExactly("&factoryBean");
197194

198195
beanNames = factory.getBeanNamesForType(List.class);
199-
assertThat(beanNames[0]).isEqualTo("factoryBean");
196+
assertThat(beanNames).containsExactly("factoryBean");
200197
}
201198

202199
@Test
@@ -381,7 +378,7 @@ static class ConfigWithBeanWithCustomName {
381378

382379
static TestBean testBean = new TestBean(ConfigWithBeanWithCustomName.class.getSimpleName());
383380

384-
@Bean(name = "customName")
381+
@Bean("customName")
385382
public TestBean methodName() {
386383
return testBean;
387384
}
@@ -405,7 +402,7 @@ static class ConfigWithBeanWithAliases {
405402

406403
static TestBean testBean = new TestBean(ConfigWithBeanWithAliases.class.getSimpleName());
407404

408-
@Bean(name = {"name1", "alias1", "alias2", "alias3"})
405+
@Bean({"name1", "alias1", "alias2", "alias3"})
409406
public TestBean methodName() {
410407
return testBean;
411408
}
@@ -430,7 +427,7 @@ static class ConfigWithBeanWithProviderImplementation implements Provider<TestBe
430427
static TestBean testBean = new TestBean(ConfigWithBeanWithProviderImplementation.class.getSimpleName());
431428

432429
@Override
433-
@Bean(name = "customName")
430+
@Bean("customName")
434431
public TestBean get() {
435432
return testBean;
436433
}
@@ -443,7 +440,7 @@ static class ConfigWithSetWithProviderImplementation implements Provider<Set<Str
443440
static Set<String> set = Collections.singleton("value");
444441

445442
@Override
446-
@Bean(name = "customName")
443+
@Bean("customName")
447444
public Set<String> get() {
448445
return set;
449446
}
@@ -453,7 +450,8 @@ public Set<String> get() {
453450
@Configuration
454451
static class ConfigWithFinalBean {
455452

456-
@Bean public final TestBean testBean() {
453+
@Bean
454+
public final TestBean testBean() {
457455
return new TestBean();
458456
}
459457
}
@@ -462,7 +460,8 @@ static class ConfigWithFinalBean {
462460
@Configuration(proxyBeanMethods = false)
463461
static class ConfigWithFinalBeanWithoutProxy {
464462

465-
@Bean public final TestBean testBean() {
463+
@Bean
464+
public final TestBean testBean() {
466465
return new TestBean();
467466
}
468467
}
@@ -471,15 +470,17 @@ static class ConfigWithFinalBeanWithoutProxy {
471470
@Configuration
472471
static class ConfigWithVoidBean {
473472

474-
@Bean public void testBean() {
473+
@Bean
474+
public void testBean() {
475475
}
476476
}
477477

478478

479479
@Configuration
480480
static class SimplestPossibleConfig {
481481

482-
@Bean public String stringBean() {
482+
@Bean
483+
public String stringBean() {
483484
return "foo";
484485
}
485486
}
@@ -488,11 +489,13 @@ static class SimplestPossibleConfig {
488489
@Configuration
489490
static class ConfigWithNonSpecificReturnTypes {
490491

491-
@Bean public Object stringBean() {
492+
@Bean
493+
public Object stringBean() {
492494
return "foo";
493495
}
494496

495-
@Bean public FactoryBean<?> factoryBean() {
497+
@Bean
498+
public FactoryBean<?> factoryBean() {
496499
ListFactoryBean fb = new ListFactoryBean();
497500
fb.setSourceList(Arrays.asList("element1", "element2"));
498501
return fb;
@@ -503,29 +506,34 @@ static class ConfigWithNonSpecificReturnTypes {
503506
@Configuration
504507
static class ConfigWithPrototypeBean {
505508

506-
@Bean public TestBean foo() {
509+
@Bean
510+
public TestBean foo() {
507511
TestBean foo = new SpousyTestBean("foo");
508512
foo.setSpouse(bar());
509513
return foo;
510514
}
511515

512-
@Bean public TestBean bar() {
516+
@Bean
517+
public TestBean bar() {
513518
TestBean bar = new SpousyTestBean("bar");
514519
bar.setSpouse(baz());
515520
return bar;
516521
}
517522

518-
@Bean @Scope("prototype")
523+
@Bean
524+
@Scope("prototype")
519525
public TestBean baz() {
520526
return new TestBean("baz");
521527
}
522528

523-
@Bean @Scope("prototype")
529+
@Bean
530+
@Scope("prototype")
524531
public TestBean adaptive1(InjectionPoint ip) {
525532
return new TestBean(ip.getMember().getName());
526533
}
527534

528-
@Bean @Scope("prototype")
535+
@Bean
536+
@Scope("prototype")
529537
public TestBean adaptive2(DependencyDescriptor dd) {
530538
return new TestBean(dd.getMember().getName());
531539
}
@@ -542,14 +550,17 @@ public TestBean bar() {
542550
}
543551

544552

553+
@SuppressWarnings("deprecation")
545554
@Configuration(enforceUniqueMethods = false)
546555
static class ConfigWithMethodNameMismatch {
547556

548-
@Bean(name = "foo") public TestBean foo1() {
557+
@Bean("foo")
558+
public TestBean foo1() {
549559
return new SpousyTestBean("foo1");
550560
}
551561

552-
@Bean(name = "foo") public TestBean foo2() {
562+
@Bean("foo")
563+
public TestBean foo2() {
553564
return new SpousyTestBean("foo2");
554565
}
555566
}
@@ -558,12 +569,14 @@ static class ConfigWithMethodNameMismatch {
558569
@Scope("prototype")
559570
static class AdaptiveInjectionPoints {
560571

561-
@Autowired @Qualifier("adaptive1")
572+
@Autowired
573+
@Qualifier("adaptive1")
562574
public TestBean adaptiveInjectionPoint1;
563575

564576
public TestBean adaptiveInjectionPoint2;
565577

566-
@Autowired @Qualifier("adaptive2")
578+
@Autowired
579+
@Qualifier("adaptive2")
567580
public void setAdaptiveInjectionPoint2(TestBean adaptiveInjectionPoint2) {
568581
this.adaptiveInjectionPoint2 = adaptiveInjectionPoint2;
569582
}
@@ -687,15 +700,16 @@ public ApplicationListener<ContextClosedEvent> listener() {
687700
}
688701

689702

703+
@SuppressWarnings("deprecation")
690704
@Configuration(enforceUniqueMethods = false)
691705
public static class OverloadedBeanMismatch {
692706

693-
@Bean(name = "other")
707+
@Bean("other")
694708
public NestedTestBean foo() {
695709
return new NestedTestBean();
696710
}
697711

698-
@Bean(name = "foo")
712+
@Bean("foo")
699713
public TestBean foo(@Qualifier("other") NestedTestBean other) {
700714
TestBean tb = new TestBean();
701715
tb.setLawyer(other);
@@ -728,7 +742,7 @@ static class AbstractPrototype implements PrototypeInterface {
728742
static class ConfigWithDynamicPrototype {
729743

730744
@Bean
731-
@Scope(value = "prototype")
745+
@Scope("prototype")
732746
public PrototypeInterface getDemoBean(int i) {
733747
return switch (i) {
734748
case 1 -> new PrototypeOne();

0 commit comments

Comments
 (0)