From b313b3395f6227659d1107fa1c6966b54b3e0fa6 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 12 May 2020 12:35:37 +0200 Subject: [PATCH] Fix AutoProxyLazyInitTests See gh-24915 --- .../annotation/AutoProxyLazyInitTests.java | 100 ++++++++++-------- 1 file changed, 57 insertions(+), 43 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/context/annotation/AutoProxyLazyInitTests.java b/spring-context/src/test/java/org/springframework/context/annotation/AutoProxyLazyInitTests.java index e256d6784fcb..c73ddf7ad59e 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/AutoProxyLazyInitTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/AutoProxyLazyInitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,83 +18,93 @@ import javax.annotation.PreDestroy; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator; import org.springframework.aop.framework.autoproxy.target.LazyInitTargetSourceCreator; import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource; -import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.event.ApplicationContextEvent; import static org.assertj.core.api.Assertions.assertThat; /** + * Integration tests for {@link BeanNameAutoProxyCreator} and + * {@link LazyInitTargetSourceCreator}. + * * @author Juergen Hoeller * @author Arrault Fabien + * @author Sam Brannen */ -public class AutoProxyLazyInitTests { +class AutoProxyLazyInitTests { - @Test - public void withStaticBeanMethod() { + @BeforeEach + void resetBeans() { MyBeanImpl.initialized = false; + } - ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStatic.class); - MyBean bean = ctx.getBean("myBean", MyBean.class); + @Test + void withStaticBeanMethod() { + ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStatic.class); + MyBean bean = ctx.getBean(MyBean.class); assertThat(MyBeanImpl.initialized).isFalse(); bean.doIt(); assertThat(MyBeanImpl.initialized).isTrue(); + + ctx.close(); } @Test - public void withStaticBeanMethodAndInterface() { - MyBeanImpl.initialized = false; - - ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStaticAndInterface.class); - MyBean bean = ctx.getBean("myBean", MyBean.class); + void withStaticBeanMethodAndInterface() { + ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStaticAndInterface.class); + MyBean bean = ctx.getBean(MyBean.class); assertThat(MyBeanImpl.initialized).isFalse(); bean.doIt(); assertThat(MyBeanImpl.initialized).isTrue(); + + ctx.close(); } @Test - public void withNonStaticBeanMethod() { - MyBeanImpl.initialized = false; - - ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStatic.class); - MyBean bean = ctx.getBean("myBean", MyBean.class); + void withNonStaticBeanMethod() { + ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStatic.class); + MyBean bean = ctx.getBean(MyBean.class); assertThat(MyBeanImpl.initialized).isFalse(); bean.doIt(); assertThat(MyBeanImpl.initialized).isTrue(); + + ctx.close(); } @Test - public void withNonStaticBeanMethodAndInterface() { - MyBeanImpl.initialized = false; - - ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class); - MyBean bean = ctx.getBean("myBean", MyBean.class); + void withNonStaticBeanMethodAndInterface() { + ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class); + MyBean bean = ctx.getBean(MyBean.class); assertThat(MyBeanImpl.initialized).isFalse(); bean.doIt(); assertThat(MyBeanImpl.initialized).isTrue(); + + ctx.close(); } - public static interface MyBean { + interface MyBean { - public String doIt(); + String doIt(); } - public static class MyBeanImpl implements MyBean { + static class MyBeanImpl implements MyBean { - public static boolean initialized = false; + static boolean initialized = false; - public MyBeanImpl() { + MyBeanImpl() { initialized = true; } @@ -110,46 +120,48 @@ public void destroy() { @Configuration - public static class ConfigWithStatic { + static class ConfigWithStatic { @Bean - public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() { + BeanNameAutoProxyCreator lazyInitAutoProxyCreator() { BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator(); + autoProxyCreator.setBeanNames("*"); autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator()); return autoProxyCreator; } @Bean - public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() { + LazyInitTargetSourceCreator lazyInitTargetSourceCreator() { return new StrictLazyInitTargetSourceCreator(); } @Bean @Lazy - public static MyBean myBean() { + static MyBean myBean() { return new MyBeanImpl(); } } @Configuration - public static class ConfigWithStaticAndInterface implements ApplicationListener { + static class ConfigWithStaticAndInterface implements ApplicationListener { @Bean - public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() { + BeanNameAutoProxyCreator lazyInitAutoProxyCreator() { BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator(); + autoProxyCreator.setBeanNames("*"); autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator()); return autoProxyCreator; } @Bean - public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() { + LazyInitTargetSourceCreator lazyInitTargetSourceCreator() { return new StrictLazyInitTargetSourceCreator(); } @Bean @Lazy - public static MyBean myBean() { + static MyBean myBean() { return new MyBeanImpl(); } @@ -160,46 +172,48 @@ public void onApplicationEvent(ApplicationContextEvent event) { @Configuration - public static class ConfigWithNonStatic { + static class ConfigWithNonStatic { @Bean - public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() { + BeanNameAutoProxyCreator lazyInitAutoProxyCreator() { BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator(); + autoProxyCreator.setBeanNames("*"); autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator()); return autoProxyCreator; } @Bean - public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() { + LazyInitTargetSourceCreator lazyInitTargetSourceCreator() { return new StrictLazyInitTargetSourceCreator(); } @Bean @Lazy - public MyBean myBean() { + MyBean myBean() { return new MyBeanImpl(); } } @Configuration - public static class ConfigWithNonStaticAndInterface implements ApplicationListener { + static class ConfigWithNonStaticAndInterface implements ApplicationListener { @Bean - public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() { + BeanNameAutoProxyCreator lazyInitAutoProxyCreator() { BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator(); + autoProxyCreator.setBeanNames("*"); autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator()); return autoProxyCreator; } @Bean - public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() { + LazyInitTargetSourceCreator lazyInitTargetSourceCreator() { return new StrictLazyInitTargetSourceCreator(); } @Bean @Lazy - public MyBean myBean() { + MyBean myBean() { return new MyBeanImpl(); }