@Override
        public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
            Assert.isInstanceOf(ConfigurableListableBeanFactory.class, registry,
                    "Embedded Database Auto-configuration can only be used with a ConfigurableListableBeanFactory");
            ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) registry;

            if (registry.containsBeanDefinition("embeddedDataSource1")) {
                registry.removeBeanDefinition("embeddedDataSource1");
            }
            if (registry.containsBeanDefinition("embeddedDataSource2")) {
                registry.removeBeanDefinition("embeddedDataSource2");
            }

            Replace replace = getDatabaseReplaceMode(environment, databaseDefinitions);
            if (replace == Replace.NONE) {
                logger.info("The use of the embedded database has been disabled");
                return;
            }

            for (DatabaseDefinition databaseDefinition : databaseDefinitions) {
                BeanDefinitionHolder dataSourceInfo = getDataSourceBeanDefinition(beanFactory, databaseDefinition);

                String dataSourceBeanName = dataSourceInfo.getBeanName();
                String contextBeanName = dataSourceBeanName + "Context";

                RootBeanDefinition contextDefinition = new RootBeanDefinition();
                contextDefinition.setBeanClass(DefaultDatabaseContext.class);
                contextDefinition.setPrimary(dataSourceInfo.getBeanDefinition().isPrimary());
                contextDefinition.getConstructorArgumentValues()
                        .addIndexedArgumentValue(0, (ObjectFactory<DatabaseProvider>) () -> {
                            ProviderResolver providerResolver = beanFactory.getBean(ProviderResolver.class);
                            DatabaseProviders databaseProviders = beanFactory.getBean(DatabaseProviders.class);
                            ProviderDescriptor providerDescriptor = providerResolver.getDescriptor(databaseDefinition);
                            return databaseProviders.getProvider(providerDescriptor);
                        });

                RootBeanDefinition dataSourceDefinition = new RootBeanDefinition();
                if (ClassUtils.hasMethod(AbstractBeanDefinition.class, "setInstanceSupplier", Supplier.class)) {
                    dataSourceDefinition.setBeanClass(DataSource.class);
                    dataSourceDefinition.setPrimary(dataSourceInfo.getBeanDefinition().isPrimary());
                    dataSourceDefinition.setInstanceSupplier(() -> {
                        DatabaseContext databaseContext = beanFactory.getBean(contextBeanName, DatabaseContext.class);
                        return ProxyFactory.getProxy(DataSource.class, new DatabaseTargetSource(databaseContext));
                    });
                } else {
                    dataSourceDefinition.setBeanClass(EmbeddedDatabaseFactoryBean.class);
                    dataSourceDefinition.setPrimary(dataSourceInfo.getBeanDefinition().isPrimary());
                    dataSourceDefinition.getConstructorArgumentValues()
                            .addIndexedArgumentValue(0, (ObjectFactory<DatabaseContext>) () ->
                                    beanFactory.getBean(contextBeanName, DatabaseContext.class));
                }

                logger.info("Replacing '{}' DataSource bean with embedded version", dataSourceBeanName);

                if (registry.containsBeanDefinition(dataSourceBeanName)) {
                    registry.removeBeanDefinition(dataSourceBeanName);
                }

                registry.registerBeanDefinition(contextBeanName, contextDefinition);
                registry.registerBeanDefinition(dataSourceBeanName, dataSourceDefinition);
            }
        }
Implementation of the {@link org.springframework.test.context.ContextCustomizerFactory} interface,
which is responsible for initialization of the embedded database and its registration to the application context.
The applied initialization strategy is driven by the {@link AutoConfigureEmbeddedDatabase} annotation.

@see AutoConfigureEmbeddedDatabase
embedded-database-spring-test/src/main/java/io/zonky/test/db/EmbeddedDatabaseContextCustomizerFactory.java