Skip to content

Commit 10c5322

Browse files
committed
Merge pull request #14907 from Dave Syer
* gh-14907: Polish "Add CachingMetadataReaderFactoryPostProcessor earlier in context lifecyle" Add CachingMetadataReaderFactoryPostProcessor earlier in context lifecyle
2 parents eafee8b + 0e08d37 commit 10c5322

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SharedMetadataReaderFactoryContextInitializer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
* @author Phillip Webb
4747
* @since 1.4.0
4848
*/
49-
class SharedMetadataReaderFactoryContextInitializer
50-
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
49+
class SharedMetadataReaderFactoryContextInitializer implements
50+
ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
5151

5252
public static final String BEAN_NAME = "org.springframework.boot.autoconfigure."
5353
+ "internalCachingMetadataReaderFactory";
@@ -58,6 +58,11 @@ public void initialize(ConfigurableApplicationContext applicationContext) {
5858
new CachingMetadataReaderFactoryPostProcessor());
5959
}
6060

61+
@Override
62+
public int getOrder() {
63+
return 0;
64+
}
65+
6166
/**
6267
* {@link BeanDefinitionRegistryPostProcessor} to register the
6368
* {@link CachingMetadataReaderFactory} and configure the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure;
18+
19+
import java.util.List;
20+
21+
import org.junit.Test;
22+
23+
import org.springframework.beans.BeansException;
24+
import org.springframework.beans.factory.config.BeanDefinition;
25+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
26+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
27+
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
28+
import org.springframework.boot.SpringApplication;
29+
import org.springframework.boot.WebApplicationType;
30+
import org.springframework.context.ApplicationContextInitializer;
31+
import org.springframework.context.support.GenericApplicationContext;
32+
import org.springframework.test.util.ReflectionTestUtils;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Tests for {@link SharedMetadataReaderFactoryContextInitializer}.
38+
*
39+
* @author Dave Syer
40+
*/
41+
public class SharedMetadataReaderFactoryContextInitializerTests {
42+
43+
@Test
44+
public void checkOrderOfInitializer() {
45+
SpringApplication application = new SpringApplication(TestConfig.class);
46+
application.setWebApplicationType(WebApplicationType.NONE);
47+
@SuppressWarnings("unchecked")
48+
List<ApplicationContextInitializer<?>> initializers = (List<ApplicationContextInitializer<?>>) ReflectionTestUtils
49+
.getField(application, "initializers");
50+
// Simulate what would happen if an initializer was added using spring.factories
51+
// and happened to be loaded first
52+
initializers.add(0, new Initializer());
53+
GenericApplicationContext context = (GenericApplicationContext) application.run();
54+
BeanDefinition definition = context.getBeanDefinition(
55+
SharedMetadataReaderFactoryContextInitializer.BEAN_NAME);
56+
assertThat(definition.getAttribute("seen")).isEqualTo(true);
57+
}
58+
59+
protected static class TestConfig {
60+
61+
}
62+
63+
static class Initializer
64+
implements ApplicationContextInitializer<GenericApplicationContext> {
65+
66+
@Override
67+
public void initialize(GenericApplicationContext applicationContext) {
68+
applicationContext.addBeanFactoryPostProcessor(new PostProcessor());
69+
}
70+
71+
}
72+
73+
static class PostProcessor implements BeanDefinitionRegistryPostProcessor {
74+
75+
@Override
76+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
77+
throws BeansException {
78+
}
79+
80+
@Override
81+
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
82+
throws BeansException {
83+
for (String name : registry.getBeanDefinitionNames()) {
84+
BeanDefinition definition = registry.getBeanDefinition(name);
85+
definition.setAttribute("seen", true);
86+
}
87+
}
88+
89+
}
90+
91+
}

0 commit comments

Comments
 (0)