spring ConnectionFactoryInitializer 源码
spring ConnectionFactoryInitializer 代码
文件路径:/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ConnectionFactoryInitializer.java
/*
* Copyright 2002-2021 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.r2dbc.connection.init;
import io.r2dbc.spi.ConnectionFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Used to {@linkplain #setDatabasePopulator set up} a database during
* initialization and {@link #setDatabaseCleaner clean up} a database during
* destruction.
*
* @author Dave Syer
* @author Sam Brannen
* @author Mark Paluch
* @since 5.3
* @see DatabasePopulator
*/
public class ConnectionFactoryInitializer implements InitializingBean, DisposableBean {
@Nullable
private ConnectionFactory connectionFactory;
@Nullable
private DatabasePopulator databasePopulator;
@Nullable
private DatabasePopulator databaseCleaner;
private boolean enabled = true;
/**
* The {@link ConnectionFactory} for the database to populate when this
* component is initialized and to clean up when this component is shut down.
* <p>This property is mandatory with no default provided.
* @param connectionFactory the R2DBC {@link ConnectionFactory}
*/
public void setConnectionFactory(ConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
/**
* Set the {@link DatabasePopulator} to execute during the bean initialization phase.
* @param databasePopulator the {@link DatabasePopulator} to use during initialization
* @see #setDatabaseCleaner
*/
public void setDatabasePopulator(DatabasePopulator databasePopulator) {
this.databasePopulator = databasePopulator;
}
/**
* Set the {@link DatabasePopulator} to execute during the bean destruction
* phase, cleaning up the database and leaving it in a known state for others.
* @param databaseCleaner the {@code DatabasePopulator} to use during destruction
* @see #setDatabasePopulator
*/
public void setDatabaseCleaner(DatabasePopulator databaseCleaner) {
this.databaseCleaner = databaseCleaner;
}
/**
* Flag to explicitly enable or disable the {@linkplain #setDatabasePopulator
* database populator} and {@linkplain #setDatabaseCleaner database cleaner}.
* @param enabled {@code true} if the database populator and database cleaner
* should be called on startup and shutdown, respectively
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
/**
* Use the {@linkplain #setDatabasePopulator database populator} to set up
* the database.
*/
@Override
public void afterPropertiesSet() {
execute(this.databasePopulator);
}
/**
* Use the {@linkplain #setDatabaseCleaner database cleaner} to clean up the
* database.
*/
@Override
public void destroy() {
execute(this.databaseCleaner);
}
private void execute(@Nullable DatabasePopulator populator) {
Assert.state(this.connectionFactory != null, "ConnectionFactory must be set");
if (this.enabled && populator != null) {
populator.populate(this.connectionFactory).block();
}
}
}
相关信息
相关文章
spring CannotReadScriptException 源码
spring CompositeDatabasePopulator 源码
spring ResourceDatabasePopulator 源码
spring ScriptParseException 源码
spring ScriptStatementFailedException 源码
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
7、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦