Skip to content

Commit 77ac09b

Browse files
committed
Move RefreshScope to a static @bean method
The test shows that this fixes a weird initialization ordering problem that leads otherwise to Spring thinking there is a dependency cycle (even though there is not). This problem only surfaces when there is a schema.sql to apply. Fixes gh-355
1 parent dbac0f0 commit 77ac09b

File tree

3 files changed

+63
-19
lines changed

3 files changed

+63
-19
lines changed

spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.java

+4-19
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.beans.factory.config.BeanDefinition;
2828
import org.springframework.beans.factory.config.BeanDefinitionHolder;
2929
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
30-
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
3130
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3231
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
3332
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@@ -62,24 +61,9 @@
6261
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
6362
public class RefreshAutoConfiguration {
6463

65-
@Component
66-
@ConditionalOnMissingBean(RefreshScope.class)
67-
protected static class RefreshScopeConfiguration
68-
implements BeanDefinitionRegistryPostProcessor {
69-
70-
@Override
71-
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
72-
throws BeansException {
73-
}
74-
75-
@Override
76-
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
77-
throws BeansException {
78-
registry.registerBeanDefinition("refreshScope",
79-
BeanDefinitionBuilder.genericBeanDefinition(RefreshScope.class)
80-
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
81-
.getBeanDefinition());
82-
}
64+
@Bean
65+
public static RefreshScope refreshScope() {
66+
return new RefreshScope();
8367
}
8468

8569
@Component
@@ -126,6 +110,7 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
126110
BeanDefinitionHolder proxy = ScopedProxyUtils
127111
.createScopedProxy(holder, registry, true);
128112
definition.setScope("refresh");
113+
definition.setLazyInit(true);
129114
registry.registerBeanDefinition(proxy.getBeanName(),
130115
proxy.getBeanDefinition());
131116
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2016-2017 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.cloud.autoconfigure;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.boot.SpringBootConfiguration;
22+
import org.springframework.boot.WebApplicationType;
23+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
25+
import org.springframework.boot.builder.SpringApplicationBuilder;
26+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
27+
import org.springframework.context.annotation.Import;
28+
29+
/**
30+
* @author Dave Syer
31+
*
32+
*/
33+
public class JdbcConfigurationTests {
34+
35+
@Test
36+
public void schemaApplied() {
37+
new SpringApplicationBuilder(BrokenApplication.class).web(WebApplicationType.NONE)
38+
.run("--spring.datasource.initialization-mode=always").close();
39+
}
40+
41+
@SpringBootConfiguration
42+
@EnableConfigurationProperties(DataSourceProperties.class)
43+
@Import({ DataSourceAutoConfiguration.class, RefreshAutoConfiguration.class })
44+
protected static class BrokenApplication {
45+
46+
public static void main(String[] args) {
47+
new SpringApplicationBuilder(BrokenApplication.class)
48+
.web(WebApplicationType.NONE).run(args);
49+
}
50+
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
INSERT INTO foos (value)
2+
VALUES
3+
('ITEM1')
4+
,('ITEM2')
5+
,('ITEM3')
6+
;

0 commit comments

Comments
 (0)