Skip to content

Commit b313b33

Browse files
committed
Fix AutoProxyLazyInitTests
See gh-24915
1 parent 3c3e8e6 commit b313b33

File tree

1 file changed

+57
-43
lines changed

1 file changed

+57
-43
lines changed

spring-context/src/test/java/org/springframework/context/annotation/AutoProxyLazyInitTests.java

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -18,83 +18,93 @@
1818

1919
import javax.annotation.PreDestroy;
2020

21+
import org.junit.jupiter.api.BeforeEach;
2122
import org.junit.jupiter.api.Test;
2223

2324
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
2425
import org.springframework.aop.framework.autoproxy.target.LazyInitTargetSourceCreator;
2526
import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource;
26-
import org.springframework.context.ApplicationContext;
2727
import org.springframework.context.ApplicationListener;
28+
import org.springframework.context.ConfigurableApplicationContext;
2829
import org.springframework.context.event.ApplicationContextEvent;
2930

3031
import static org.assertj.core.api.Assertions.assertThat;
3132

3233
/**
34+
* Integration tests for {@link BeanNameAutoProxyCreator} and
35+
* {@link LazyInitTargetSourceCreator}.
36+
*
3337
* @author Juergen Hoeller
3438
* @author Arrault Fabien
39+
* @author Sam Brannen
3540
*/
36-
public class AutoProxyLazyInitTests {
41+
class AutoProxyLazyInitTests {
3742

38-
@Test
39-
public void withStaticBeanMethod() {
43+
@BeforeEach
44+
void resetBeans() {
4045
MyBeanImpl.initialized = false;
46+
}
4147

42-
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStatic.class);
43-
MyBean bean = ctx.getBean("myBean", MyBean.class);
48+
@Test
49+
void withStaticBeanMethod() {
50+
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStatic.class);
51+
MyBean bean = ctx.getBean(MyBean.class);
4452

4553
assertThat(MyBeanImpl.initialized).isFalse();
4654
bean.doIt();
4755
assertThat(MyBeanImpl.initialized).isTrue();
56+
57+
ctx.close();
4858
}
4959

5060
@Test
51-
public void withStaticBeanMethodAndInterface() {
52-
MyBeanImpl.initialized = false;
53-
54-
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStaticAndInterface.class);
55-
MyBean bean = ctx.getBean("myBean", MyBean.class);
61+
void withStaticBeanMethodAndInterface() {
62+
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStaticAndInterface.class);
63+
MyBean bean = ctx.getBean(MyBean.class);
5664

5765
assertThat(MyBeanImpl.initialized).isFalse();
5866
bean.doIt();
5967
assertThat(MyBeanImpl.initialized).isTrue();
68+
69+
ctx.close();
6070
}
6171

6272
@Test
63-
public void withNonStaticBeanMethod() {
64-
MyBeanImpl.initialized = false;
65-
66-
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStatic.class);
67-
MyBean bean = ctx.getBean("myBean", MyBean.class);
73+
void withNonStaticBeanMethod() {
74+
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStatic.class);
75+
MyBean bean = ctx.getBean(MyBean.class);
6876

6977
assertThat(MyBeanImpl.initialized).isFalse();
7078
bean.doIt();
7179
assertThat(MyBeanImpl.initialized).isTrue();
80+
81+
ctx.close();
7282
}
7383

7484
@Test
75-
public void withNonStaticBeanMethodAndInterface() {
76-
MyBeanImpl.initialized = false;
77-
78-
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class);
79-
MyBean bean = ctx.getBean("myBean", MyBean.class);
85+
void withNonStaticBeanMethodAndInterface() {
86+
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class);
87+
MyBean bean = ctx.getBean(MyBean.class);
8088

8189
assertThat(MyBeanImpl.initialized).isFalse();
8290
bean.doIt();
8391
assertThat(MyBeanImpl.initialized).isTrue();
92+
93+
ctx.close();
8494
}
8595

8696

87-
public static interface MyBean {
97+
interface MyBean {
8898

89-
public String doIt();
99+
String doIt();
90100
}
91101

92102

93-
public static class MyBeanImpl implements MyBean {
103+
static class MyBeanImpl implements MyBean {
94104

95-
public static boolean initialized = false;
105+
static boolean initialized = false;
96106

97-
public MyBeanImpl() {
107+
MyBeanImpl() {
98108
initialized = true;
99109
}
100110

@@ -110,46 +120,48 @@ public void destroy() {
110120

111121

112122
@Configuration
113-
public static class ConfigWithStatic {
123+
static class ConfigWithStatic {
114124

115125
@Bean
116-
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
126+
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
117127
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
128+
autoProxyCreator.setBeanNames("*");
118129
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
119130
return autoProxyCreator;
120131
}
121132

122133
@Bean
123-
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
134+
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
124135
return new StrictLazyInitTargetSourceCreator();
125136
}
126137

127138
@Bean
128139
@Lazy
129-
public static MyBean myBean() {
140+
static MyBean myBean() {
130141
return new MyBeanImpl();
131142
}
132143
}
133144

134145

135146
@Configuration
136-
public static class ConfigWithStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
147+
static class ConfigWithStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
137148

138149
@Bean
139-
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
150+
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
140151
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
152+
autoProxyCreator.setBeanNames("*");
141153
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
142154
return autoProxyCreator;
143155
}
144156

145157
@Bean
146-
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
158+
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
147159
return new StrictLazyInitTargetSourceCreator();
148160
}
149161

150162
@Bean
151163
@Lazy
152-
public static MyBean myBean() {
164+
static MyBean myBean() {
153165
return new MyBeanImpl();
154166
}
155167

@@ -160,46 +172,48 @@ public void onApplicationEvent(ApplicationContextEvent event) {
160172

161173

162174
@Configuration
163-
public static class ConfigWithNonStatic {
175+
static class ConfigWithNonStatic {
164176

165177
@Bean
166-
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
178+
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
167179
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
180+
autoProxyCreator.setBeanNames("*");
168181
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
169182
return autoProxyCreator;
170183
}
171184

172185
@Bean
173-
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
186+
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
174187
return new StrictLazyInitTargetSourceCreator();
175188
}
176189

177190
@Bean
178191
@Lazy
179-
public MyBean myBean() {
192+
MyBean myBean() {
180193
return new MyBeanImpl();
181194
}
182195
}
183196

184197

185198
@Configuration
186-
public static class ConfigWithNonStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
199+
static class ConfigWithNonStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
187200

188201
@Bean
189-
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
202+
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
190203
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
204+
autoProxyCreator.setBeanNames("*");
191205
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
192206
return autoProxyCreator;
193207
}
194208

195209
@Bean
196-
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
210+
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
197211
return new StrictLazyInitTargetSourceCreator();
198212
}
199213

200214
@Bean
201215
@Lazy
202-
public MyBean myBean() {
216+
MyBean myBean() {
203217
return new MyBeanImpl();
204218
}
205219

0 commit comments

Comments
 (0)