Clarify scope of DataSourceInitializedEvent

This commit clarifies that DataSourceInitializedEvent is only to be
used by the datasource initializer facility and JPA (Hibernate). The
even is renamed to DataSourceSchemaCreatedEvent to clarify what it
actually signals.

Closes gh-4292
pull/9508/merge
Stephane Nicoll 7 years ago
parent 6635b6aa99
commit b322b1943b

@ -29,13 +29,13 @@ import org.springframework.context.ApplicationListener;
/** /**
* Bean to handle {@link DataSource} initialization by running {@literal schema-*.sql} on * Bean to handle {@link DataSource} initialization by running {@literal schema-*.sql} on
* {@link InitializingBean#afterPropertiesSet()} and {@literal data-*.sql} SQL scripts on * {@link InitializingBean#afterPropertiesSet()} and {@literal data-*.sql} SQL scripts on
* a {@link DataSourceInitializedEvent}. * a {@link DataSourceSchemaCreatedEvent}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @see DataSourceAutoConfiguration * @see DataSourceAutoConfiguration
*/ */
class DataSourceInitializerInvoker class DataSourceInitializerInvoker
implements ApplicationListener<DataSourceInitializedEvent>, InitializingBean { implements ApplicationListener<DataSourceSchemaCreatedEvent>, InitializingBean {
private static final Log logger = LogFactory.getLog(DataSourceInitializerInvoker.class); private static final Log logger = LogFactory.getLog(DataSourceInitializerInvoker.class);
@ -65,7 +65,7 @@ class DataSourceInitializerInvoker
if (schemaCreated) { if (schemaCreated) {
try { try {
this.applicationContext this.applicationContext
.publishEvent(new DataSourceInitializedEvent( .publishEvent(new DataSourceSchemaCreatedEvent(
initializer.getDataSource())); initializer.getDataSource()));
// The listener might not be registered yet, so don't rely on it. // The listener might not be registered yet, so don't rely on it.
if (!this.initialized) { if (!this.initialized) {
@ -82,7 +82,7 @@ class DataSourceInitializerInvoker
} }
@Override @Override
public void onApplicationEvent(DataSourceInitializedEvent event) { public void onApplicationEvent(DataSourceSchemaCreatedEvent event) {
// NOTE the event can happen more than once and // NOTE the event can happen more than once and
// the event datasource is not used here // the event datasource is not used here
DataSourceInitializer initializer = getDataSourceInitializer(); DataSourceInitializer initializer = getDataSourceInitializer();

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,22 +21,21 @@ import javax.sql.DataSource;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
/** /**
* {@link ApplicationEvent} used internally to trigger {@link DataSource} initialization. * {@link ApplicationEvent} used internally to indicate that the schema of a new
* Initialization can occur when {@literal schema-*.sql} files are executed or when * {@link DataSource} has been created. This happens when {@literal schema-*.sql} files
* external libraries (e.g. JPA) initialize the database. * are executed or when Hibernate initializes the database.
* *
* @author Dave Syer * @author Dave Syer
* @see DataSourceInitializer
* @since 1.1.0 * @since 1.1.0
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class DataSourceInitializedEvent extends ApplicationEvent { public class DataSourceSchemaCreatedEvent extends ApplicationEvent {
/** /**
* Create a new {@link DataSourceInitializedEvent}. * Create a new {@link DataSourceSchemaCreatedEvent}.
* @param source the source {@link DataSource}. * @param source the source {@link DataSource}.
*/ */
public DataSourceInitializedEvent(DataSource source) { public DataSourceSchemaCreatedEvent(DataSource source) {
super(source); super(source);
} }

@ -27,15 +27,15 @@ import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.autoconfigure.jdbc.DataSourceInitializedEvent; import org.springframework.boot.autoconfigure.jdbc.DataSourceSchemaCreatedEvent;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection; import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
/** /**
* {@link BeanPostProcessor} used to fire {@link DataSourceInitializedEvent}s. Should only * {@link BeanPostProcessor} used to fire {@link DataSourceSchemaCreatedEvent}s. Should
* be registered via the inner {@link Registrar} class. * only be registered via the inner {@link Registrar} class.
* *
* @author Dave Syer * @author Dave Syer
* @since 1.1.0 * @since 1.1.0
@ -75,7 +75,7 @@ class DataSourceInitializedPublisher implements BeanPostProcessor {
DataSource dataSource = findDataSource(entityManagerFactory); DataSource dataSource = findDataSource(entityManagerFactory);
if (dataSource != null && isInitializingDatabase(dataSource)) { if (dataSource != null && isInitializingDatabase(dataSource)) {
this.applicationContext this.applicationContext
.publishEvent(new DataSourceInitializedEvent(dataSource)); .publishEvent(new DataSourceSchemaCreatedEvent(dataSource));
} }
} }

@ -177,7 +177,7 @@ public class DataSourceInitializerInvokerTests {
return context -> { return context -> {
assertThat(context).hasSingleBean(DataSource.class); assertThat(context).hasSingleBean(DataSource.class);
DataSource dataSource = context.getBean(DataSource.class); DataSource dataSource = context.getBean(DataSource.class);
context.publishEvent(new DataSourceInitializedEvent(dataSource)); context.publishEvent(new DataSourceSchemaCreatedEvent(dataSource));
assertDataSourceNotInitialized(dataSource); assertDataSourceNotInitialized(dataSource);
}; };
} }

Loading…
Cancel
Save