Skip to content

Commit c569450

Browse files
committed
Early configuration of ApplicationStartup on BeanFactory
Prior to this commit, the `GenericApplicationContext` configured the `AppliationStartup` on the `BeanFactory` only right before refreshing it. Delaying this has no purpose and we should instead configure it as soon as possible by overriding the setter method. Closes gh-25718
1 parent 3fd89c3 commit c569450

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.core.io.Resource;
3737
import org.springframework.core.io.ResourceLoader;
3838
import org.springframework.core.io.support.ResourcePatternResolver;
39+
import org.springframework.core.metrics.ApplicationStartup;
3940
import org.springframework.lang.Nullable;
4041
import org.springframework.util.Assert;
4142

@@ -158,6 +159,12 @@ public void setParent(@Nullable ApplicationContext parent) {
158159
this.beanFactory.setParentBeanFactory(getInternalParentBeanFactory());
159160
}
160161

162+
@Override
163+
public void setApplicationStartup(ApplicationStartup applicationStartup) {
164+
super.setApplicationStartup(applicationStartup);
165+
this.beanFactory.setApplicationStartup(applicationStartup);
166+
}
167+
161168
/**
162169
* Set whether it should be allowed to override bean definitions by registering
163170
* a different definition with the same name, automatically replacing the former.
@@ -266,7 +273,6 @@ protected final void refreshBeanFactory() throws IllegalStateException {
266273
throw new IllegalStateException(
267274
"GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
268275
}
269-
this.beanFactory.setApplicationStartup(this.getApplicationStartup());
270276
this.beanFactory.setSerializationId(getId());
271277
}
272278

spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java

Lines changed: 21 additions & 12 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.
@@ -23,6 +23,7 @@
2323
import org.springframework.beans.factory.support.RootBeanDefinition;
2424
import org.springframework.context.ApplicationContext;
2525
import org.springframework.context.ApplicationContextAware;
26+
import org.springframework.core.metrics.jfr.FlightRecorderApplicationStartup;
2627
import org.springframework.util.ObjectUtils;
2728

2829
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,7 +37,7 @@
3637
public class GenericApplicationContextTests {
3738

3839
@Test
39-
public void getBeanForClass() {
40+
void getBeanForClass() {
4041
GenericApplicationContext ac = new GenericApplicationContext();
4142
ac.registerBeanDefinition("testBean", new RootBeanDefinition(String.class));
4243
ac.refresh();
@@ -50,7 +51,7 @@ public void getBeanForClass() {
5051
}
5152

5253
@Test
53-
public void withSingletonSupplier() {
54+
void withSingletonSupplier() {
5455
GenericApplicationContext ac = new GenericApplicationContext();
5556
ac.registerBeanDefinition("testBean", new RootBeanDefinition(String.class, ac::toString));
5657
ac.refresh();
@@ -62,7 +63,7 @@ public void withSingletonSupplier() {
6263
}
6364

6465
@Test
65-
public void withScopedSupplier() {
66+
void withScopedSupplier() {
6667
GenericApplicationContext ac = new GenericApplicationContext();
6768
ac.registerBeanDefinition("testBean",
6869
new RootBeanDefinition(String.class, BeanDefinition.SCOPE_PROTOTYPE, ac::toString));
@@ -75,7 +76,7 @@ public void withScopedSupplier() {
7576
}
7677

7778
@Test
78-
public void accessAfterClosing() {
79+
void accessAfterClosing() {
7980
GenericApplicationContext ac = new GenericApplicationContext();
8081
ac.registerBeanDefinition("testBean", new RootBeanDefinition(String.class));
8182
ac.refresh();
@@ -95,7 +96,7 @@ public void accessAfterClosing() {
9596
}
9697

9798
@Test
98-
public void individualBeans() {
99+
void individualBeans() {
99100
GenericApplicationContext context = new GenericApplicationContext();
100101
context.registerBean(BeanA.class);
101102
context.registerBean(BeanB.class);
@@ -108,7 +109,7 @@ public void individualBeans() {
108109
}
109110

110111
@Test
111-
public void individualNamedBeans() {
112+
void individualNamedBeans() {
112113
GenericApplicationContext context = new GenericApplicationContext();
113114
context.registerBean("a", BeanA.class);
114115
context.registerBean("b", BeanB.class);
@@ -121,7 +122,7 @@ public void individualNamedBeans() {
121122
}
122123

123124
@Test
124-
public void individualBeanWithSupplier() {
125+
void individualBeanWithSupplier() {
125126
GenericApplicationContext context = new GenericApplicationContext();
126127
context.registerBean(BeanA.class,
127128
() -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class)));
@@ -139,7 +140,7 @@ public void individualBeanWithSupplier() {
139140
}
140141

141142
@Test
142-
public void individualBeanWithSupplierAndCustomizer() {
143+
void individualBeanWithSupplierAndCustomizer() {
143144
GenericApplicationContext context = new GenericApplicationContext();
144145
context.registerBean(BeanA.class,
145146
() -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class)),
@@ -155,7 +156,7 @@ public void individualBeanWithSupplierAndCustomizer() {
155156
}
156157

157158
@Test
158-
public void individualNamedBeanWithSupplier() {
159+
void individualNamedBeanWithSupplier() {
159160
GenericApplicationContext context = new GenericApplicationContext();
160161
context.registerBean("a", BeanA.class,
161162
() -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class)));
@@ -170,7 +171,7 @@ public void individualNamedBeanWithSupplier() {
170171
}
171172

172173
@Test
173-
public void individualNamedBeanWithSupplierAndCustomizer() {
174+
void individualNamedBeanWithSupplierAndCustomizer() {
174175
GenericApplicationContext context = new GenericApplicationContext();
175176
context.registerBean("a", BeanA.class,
176177
() -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class)),
@@ -186,7 +187,7 @@ public void individualNamedBeanWithSupplierAndCustomizer() {
186187
}
187188

188189
@Test
189-
public void individualBeanWithNullReturningSupplier() {
190+
void individualBeanWithNullReturningSupplier() {
190191
GenericApplicationContext context = new GenericApplicationContext();
191192
context.registerBean("a", BeanA.class, () -> null);
192193
context.registerBean("b", BeanB.class, BeanB::new);
@@ -201,6 +202,14 @@ public void individualBeanWithNullReturningSupplier() {
201202
assertThat(context.getBeansOfType(BeanC.class).values().iterator().next()).isSameAs(context.getBean(BeanC.class));
202203
}
203204

205+
@Test
206+
void configureApplicationStartupOnBeanFactory() {
207+
FlightRecorderApplicationStartup applicationStartup = new FlightRecorderApplicationStartup();
208+
GenericApplicationContext context = new GenericApplicationContext();
209+
context.setApplicationStartup(applicationStartup);
210+
assertThat(context.getBeanFactory().getApplicationStartup()).isEqualTo(applicationStartup);
211+
}
212+
204213

205214
static class BeanA {
206215

0 commit comments

Comments
 (0)