|
58 | 58 | import org.springframework.context.event.ContextRefreshedEvent;
|
59 | 59 | import org.springframework.context.support.GenericApplicationContext;
|
60 | 60 | import org.springframework.tests.sample.beans.ITestBean;
|
| 61 | +import org.springframework.tests.sample.beans.NestedTestBean; |
61 | 62 | import org.springframework.tests.sample.beans.TestBean;
|
62 | 63 |
|
63 | 64 | import static org.junit.Assert.*;
|
@@ -206,40 +207,50 @@ public void configurationWithPrototypeScopedBeans() {
|
206 | 207 |
|
207 | 208 | @Test
|
208 | 209 | public void configurationWithAdaptivePrototypes() {
|
209 |
| - AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext(); |
210 |
| - factory.register(ConfigWithPrototypeBean.class, AdaptiveInjectionPoints.class); |
211 |
| - factory.refresh(); |
| 210 | + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
| 211 | + ctx.register(ConfigWithPrototypeBean.class, AdaptiveInjectionPoints.class); |
| 212 | + ctx.refresh(); |
212 | 213 |
|
213 |
| - AdaptiveInjectionPoints adaptive = factory.getBean(AdaptiveInjectionPoints.class); |
| 214 | + AdaptiveInjectionPoints adaptive = ctx.getBean(AdaptiveInjectionPoints.class); |
214 | 215 | assertEquals("adaptiveInjectionPoint1", adaptive.adaptiveInjectionPoint1.getName());
|
215 | 216 | assertEquals("setAdaptiveInjectionPoint2", adaptive.adaptiveInjectionPoint2.getName());
|
216 | 217 |
|
217 |
| - adaptive = factory.getBean(AdaptiveInjectionPoints.class); |
| 218 | + adaptive = ctx.getBean(AdaptiveInjectionPoints.class); |
218 | 219 | assertEquals("adaptiveInjectionPoint1", adaptive.adaptiveInjectionPoint1.getName());
|
219 | 220 | assertEquals("setAdaptiveInjectionPoint2", adaptive.adaptiveInjectionPoint2.getName());
|
220 |
| - factory.close(); |
| 221 | + ctx.close(); |
221 | 222 | }
|
222 | 223 |
|
223 | 224 | @Test
|
224 | 225 | public void configurationWithPostProcessor() {
|
225 |
| - AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext(); |
226 |
| - factory.register(ConfigWithPostProcessor.class); |
| 226 | + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
| 227 | + ctx.register(ConfigWithPostProcessor.class); |
227 | 228 | RootBeanDefinition placeholderConfigurer = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
|
228 | 229 | placeholderConfigurer.getPropertyValues().add("properties", "myProp=myValue");
|
229 |
| - factory.registerBeanDefinition("placeholderConfigurer", placeholderConfigurer); |
230 |
| - factory.refresh(); |
| 230 | + ctx.registerBeanDefinition("placeholderConfigurer", placeholderConfigurer); |
| 231 | + ctx.refresh(); |
231 | 232 |
|
232 |
| - TestBean foo = factory.getBean("foo", TestBean.class); |
233 |
| - ITestBean bar = factory.getBean("bar", ITestBean.class); |
234 |
| - ITestBean baz = factory.getBean("baz", ITestBean.class); |
| 233 | + TestBean foo = ctx.getBean("foo", TestBean.class); |
| 234 | + ITestBean bar = ctx.getBean("bar", ITestBean.class); |
| 235 | + ITestBean baz = ctx.getBean("baz", ITestBean.class); |
235 | 236 |
|
236 | 237 | assertEquals("foo-processed-myValue", foo.getName());
|
237 | 238 | assertEquals("bar-processed-myValue", bar.getName());
|
238 | 239 | assertEquals("baz-processed-myValue", baz.getName());
|
239 | 240 |
|
240 |
| - SpousyTestBean listener = factory.getBean("listenerTestBean", SpousyTestBean.class); |
| 241 | + SpousyTestBean listener = ctx.getBean("listenerTestBean", SpousyTestBean.class); |
241 | 242 | assertTrue(listener.refreshed);
|
242 |
| - factory.close(); |
| 243 | + ctx.close(); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + public void configurationWithFunctionalRegistration() { |
| 248 | + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
| 249 | + ctx.register(ConfigWithFunctionalRegistration.class); |
| 250 | + ctx.refresh(); |
| 251 | + |
| 252 | + assertSame(ctx.getBean("spouse"), ctx.getBean(TestBean.class).getSpouse()); |
| 253 | + assertEquals("functional", ctx.getBean(NestedTestBean.class).getCompany()); |
243 | 254 | }
|
244 | 255 |
|
245 | 256 |
|
@@ -419,7 +430,6 @@ public void setAdaptiveInjectionPoint2(TestBean adaptiveInjectionPoint2) {
|
419 | 430 | }
|
420 | 431 |
|
421 | 432 |
|
422 |
| - @SuppressWarnings("unused") |
423 | 433 | static class ConfigWithPostProcessor extends ConfigWithPrototypeBean {
|
424 | 434 |
|
425 | 435 | @Value("${myProp}")
|
@@ -496,4 +506,23 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
|
496 | 506 | }
|
497 | 507 | }
|
498 | 508 |
|
| 509 | + |
| 510 | + @Configuration |
| 511 | + static class ConfigWithFunctionalRegistration { |
| 512 | + |
| 513 | + @Autowired |
| 514 | + void register(GenericApplicationContext ctx) { |
| 515 | + ctx.registerBean("spouse", TestBean.class, |
| 516 | + () -> new TestBean("functional")); |
| 517 | + ctx.registerBean(TestBean.class, |
| 518 | + () -> new TestBean(ctx.getBean("spouse", TestBean.class)), |
| 519 | + bd -> bd.setPrimary(true)); |
| 520 | + } |
| 521 | + |
| 522 | + @Bean |
| 523 | + public NestedTestBean nestedTestBean(TestBean testBean) { |
| 524 | + return new NestedTestBean(testBean.getSpouse().getName()); |
| 525 | + } |
| 526 | + } |
| 527 | + |
499 | 528 | }
|
0 commit comments