Workaround for data source cycle with hibernate

The JpaInvokerConfiguration (nested in RefreshAutoConfiguration can be
removed when and if the issue is resolved in Boot.

Also splits the hibernate test dependencies out into a separate module
so the main context tests run quicker.

Fixes gh-355
This commit is contained in:
Dave Syer
2018-05-11 09:37:07 +01:00
parent f7519a30ad
commit 0e259eab35
18 changed files with 1327 additions and 32 deletions

View File

@@ -21,20 +21,23 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.PostConstruct;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.cloud.context.refresh.ContextRefresher;
@@ -44,8 +47,10 @@ import org.springframework.cloud.logging.LoggingRebinder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.weaving.LoadTimeWeaverAware;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.instrument.classloading.LoadTimeWeaver;
import org.springframework.stereotype.Component;
/**
@@ -58,28 +63,34 @@ import org.springframework.stereotype.Component;
@Configuration
@ConditionalOnClass(RefreshScope.class)
@ConditionalOnProperty(name = "spring.cloud.refresh.enabled", matchIfMissing = true)
// TODO: support reactive
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
@AutoConfigureBefore(HibernateJpaAutoConfiguration.class)
public class RefreshAutoConfiguration {
@Component
@Bean
@ConditionalOnMissingBean(RefreshScope.class)
protected static class RefreshScopeConfiguration
implements BeanDefinitionRegistryPostProcessor {
public static RefreshScope refreshScope() {
return new RefreshScope();
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
@Configuration
@ConditionalOnClass(name = "javax.persistence.EntityManagerFactory")
protected static class JpaInvokerConfiguration implements LoadTimeWeaverAware {
@Autowired
private ListableBeanFactory beanFactory;
@PostConstruct
public void init() {
String cls = "org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker";
if (beanFactory.containsBean(cls)) {
beanFactory.getBean(cls);
}
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
throws BeansException {
registry.registerBeanDefinition("refreshScope",
BeanDefinitionBuilder.genericBeanDefinition(RefreshScope.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
.getBeanDefinition());
public void setLoadTimeWeaver(LoadTimeWeaver ltw) {
}
}
@Component

View File

@@ -20,8 +20,52 @@ import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.springframework.cloud.autoconfigure.LifecycleMvcAutoConfigurationTests;
import org.springframework.cloud.autoconfigure.RefreshAutoConfigurationClassPathTests;
import org.springframework.cloud.autoconfigure.RefreshAutoConfigurationMoreClassPathTests;
import org.springframework.cloud.autoconfigure.RefreshAutoConfigurationTests;
import org.springframework.cloud.bootstrap.BootstrapDisabledAutoConfigurationIntegrationTests;
import org.springframework.cloud.bootstrap.BootstrapOrderingAutoConfigurationIntegrationTests;
import org.springframework.cloud.bootstrap.BootstrapOrderingCustomPropertySourceIntegrationTests;
import org.springframework.cloud.bootstrap.BootstrapOrderingSpringApplicationJsonIntegrationTests;
import org.springframework.cloud.bootstrap.BootstrapSourcesOrderingTests;
import org.springframework.cloud.bootstrap.MessageSourceConfigurationTests;
import org.springframework.cloud.bootstrap.config.BootstrapConfigurationTests;
import org.springframework.cloud.bootstrap.config.BootstrapListenerHierarchyIntegrationTests;
import org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfigurationTests;
import org.springframework.cloud.bootstrap.encrypt.EncryptionIntegrationTests;
import org.springframework.cloud.bootstrap.encrypt.EncryptorFactoryTests;
import org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializerTests;
import org.springframework.cloud.bootstrap.encrypt.RsaDisabledTests;
import org.springframework.cloud.context.environment.EnvironmentManagerIntegrationTests;
import org.springframework.cloud.context.environment.EnvironmentManagerTest;
import org.springframework.cloud.context.named.NamedContextFactoryTests;
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinderIntegrationTests;
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinderLifecycleIntegrationTests;
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinderListIntegrationTests;
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinderProxyIntegrationTests;
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinderRefreshScopeIntegrationTests;
import org.springframework.cloud.context.refresh.ContextRefresherIntegrationTests;
import org.springframework.cloud.context.refresh.ContextRefresherTests;
import org.springframework.cloud.context.restart.RestartIntegrationTests;
import org.springframework.cloud.context.scope.refresh.ImportRefreshScopeIntegrationTests;
import org.springframework.cloud.context.scope.refresh.MoreRefreshScopeIntegrationTests;
import org.springframework.cloud.context.scope.refresh.RefreshEndpointIntegrationTests;
import org.springframework.cloud.context.scope.refresh.RefreshScopeConcurrencyTests;
import org.springframework.cloud.context.scope.refresh.RefreshScopeConfigurationScaleTests;
import org.springframework.cloud.context.scope.refresh.RefreshScopeConfigurationTests;
import org.springframework.cloud.context.scope.refresh.RefreshScopeIntegrationTests;
import org.springframework.cloud.context.scope.refresh.RefreshScopeLazyIntegrationTests;
import org.springframework.cloud.context.scope.refresh.RefreshScopeListBindingIntegrationTests;
import org.springframework.cloud.context.scope.refresh.RefreshScopeNullBeanIntegrationTests;
import org.springframework.cloud.context.scope.refresh.RefreshScopePureScaleTests;
import org.springframework.cloud.context.scope.refresh.RefreshScopeScaleTests;
import org.springframework.cloud.context.scope.refresh.RefreshScopeSerializationTests;
import org.springframework.cloud.context.scope.refresh.RefreshScopeWebIntegrationTests;
import org.springframework.cloud.endpoint.RefreshEndpointTests;
import org.springframework.cloud.health.RefreshScopeHealthIndicatorTests;
import org.springframework.cloud.logging.LoggingRebinderTests;
/**
* A test suite for probing weird ordering problems in the tests.
@@ -29,7 +73,39 @@ import org.springframework.cloud.endpoint.RefreshEndpointTests;
* @author Dave Syer
*/
@RunWith(Suite.class)
@SuiteClasses({ RefreshEndpointTests.class, ContextRefresherTests.class })
@SuiteClasses({ BootstrapSourcesOrderingTests.class,
BootstrapDisabledAutoConfigurationIntegrationTests.class,
EncryptionBootstrapConfigurationTests.class, RsaDisabledTests.class,
EncryptorFactoryTests.class, EnvironmentDecryptApplicationInitializerTests.class,
EncryptionIntegrationTests.class,
BootstrapOrderingAutoConfigurationIntegrationTests.class,
MessageSourceConfigurationTests.class,
BootstrapOrderingSpringApplicationJsonIntegrationTests.class,
BootstrapConfigurationTests.class,
BootstrapListenerHierarchyIntegrationTests.class,
BootstrapOrderingCustomPropertySourceIntegrationTests.class,
RefreshEndpointTests.class, RefreshAutoConfigurationClassPathTests.class,
RefreshAutoConfigurationMoreClassPathTests.class,
LifecycleMvcAutoConfigurationTests.class, RefreshAutoConfigurationTests.class,
RestartIntegrationTests.class, ContextRefresherIntegrationTests.class,
ContextRefresherTests.class,
ConfigurationPropertiesRebinderListIntegrationTests.class,
ConfigurationPropertiesRebinderLifecycleIntegrationTests.class,
ConfigurationPropertiesRebinderIntegrationTests.class,
ConfigurationPropertiesRebinderRefreshScopeIntegrationTests.class,
ConfigurationPropertiesRebinderProxyIntegrationTests.class,
RefreshScopeIntegrationTests.class, RefreshScopeConfigurationTests.class,
RefreshScopeLazyIntegrationTests.class, ImportRefreshScopeIntegrationTests.class,
RefreshScopeConcurrencyTests.class, RefreshEndpointIntegrationTests.class,
RefreshScopeConfigurationScaleTests.class,
RefreshScopeNullBeanIntegrationTests.class,
MoreRefreshScopeIntegrationTests.class,
RefreshScopeListBindingIntegrationTests.class,
RefreshScopeWebIntegrationTests.class, RefreshScopePureScaleTests.class,
RefreshScopeScaleTests.class, RefreshScopeSerializationTests.class,
NamedContextFactoryTests.class, EnvironmentManagerIntegrationTests.class,
EnvironmentManagerTest.class, LoggingRebinderTests.class,
RefreshScopeHealthIndicatorTests.class })
@Ignore
public class AdhocTestSuite {

View File

@@ -74,7 +74,7 @@ public class EncryptionBootstrapConfigurationTests {
@Test
public void rsaProperties() {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
EncryptionBootstrapConfiguration.class).web(false).properties(
EncryptionBootstrapConfiguration.class).web(WebApplicationType.NONE).properties(
"encrypt.key-store.location:classpath:/server.jks",
"encrypt.key-store.password:letmein",
"encrypt.key-store.alias:mytestkey", "encrypt.key-store.secret:changeme",

View File

@@ -22,6 +22,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.test.ClassPathExclusions;
import org.springframework.cloud.test.ModifiedClassPathRunner;
@@ -41,8 +42,8 @@ public class RsaDisabledTests {
@Before
public void setUp() {
context = new SpringApplicationBuilder().web(false)
.sources(EncryptionBootstrapConfiguration.class).web(false).properties(
context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.sources(EncryptionBootstrapConfiguration.class).web(WebApplicationType.NONE).properties(
"encrypt.key:mykey",
"encrypt.rsa.strong:true",
"encrypt.rsa.salt:foobar").run();

View File

@@ -209,6 +209,7 @@ public class RefreshScopeIntegrationTests {
}
}
@SuppressWarnings("serial")
public static class ServiceException extends Exception {}
@Configuration

View File

@@ -1,4 +0,0 @@
create table foos (
id INTEGER IDENTITY PRIMARY KEY,
value VARCHAR(30)
);