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
* {@link InitializingBean#afterPropertiesSet()} and {@literal data-*.sql} SQL scripts on
* a {@link DataSourceInitializedEvent}.
* a {@link DataSourceSchemaCreatedEvent}.
*
* @author Stephane Nicoll
* @see DataSourceAutoConfiguration
*/
class DataSourceInitializerInvoker
implements ApplicationListener<DataSourceInitializedEvent>, InitializingBean {
implements ApplicationListener<DataSourceSchemaCreatedEvent>, InitializingBean {
private static final Log logger = LogFactory.getLog(DataSourceInitializerInvoker.class);
@ -65,7 +65,7 @@ class DataSourceInitializerInvoker
if (schemaCreated) {
try {
this.applicationContext
.publishEvent(new DataSourceInitializedEvent(
.publishEvent(new DataSourceSchemaCreatedEvent(
initializer.getDataSource()));
// The listener might not be registered yet, so don't rely on it.
if (!this.initialized) {
@ -82,7 +82,7 @@ class DataSourceInitializerInvoker
}
@Override
public void onApplicationEvent(DataSourceInitializedEvent event) {
public void onApplicationEvent(DataSourceSchemaCreatedEvent event) {
// NOTE the event can happen more than once and
// the event datasource is not used here
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");
* 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;
/**
* {@link ApplicationEvent} used internally to trigger {@link DataSource} initialization.
* Initialization can occur when {@literal schema-*.sql} files are executed or when
* external libraries (e.g. JPA) initialize the database.
* {@link ApplicationEvent} used internally to indicate that the schema of a new
* {@link DataSource} has been created. This happens when {@literal schema-*.sql} files
* are executed or when Hibernate initializes the database.
*
* @author Dave Syer
* @see DataSourceInitializer
* @since 1.1.0
*/
@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}.
*/
public DataSourceInitializedEvent(DataSource source) {
public DataSourceSchemaCreatedEvent(DataSource 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.support.BeanDefinitionRegistry;
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.context.ApplicationContext;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
/**
* {@link BeanPostProcessor} used to fire {@link DataSourceInitializedEvent}s. Should only
* be registered via the inner {@link Registrar} class.
* {@link BeanPostProcessor} used to fire {@link DataSourceSchemaCreatedEvent}s. Should
* only be registered via the inner {@link Registrar} class.
*
* @author Dave Syer
* @since 1.1.0
@ -75,7 +75,7 @@ class DataSourceInitializedPublisher implements BeanPostProcessor {
DataSource dataSource = findDataSource(entityManagerFactory);
if (dataSource != null && isInitializingDatabase(dataSource)) {
this.applicationContext
.publishEvent(new DataSourceInitializedEvent(dataSource));
.publishEvent(new DataSourceSchemaCreatedEvent(dataSource));
}
}

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

Loading…
Cancel
Save