org.zodiac.autoconfigure.lock.database.DatabaseLockAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.lock.database;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
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.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.zodiac.autoconfigure.lock.LockAutoConfiguration;
import org.zodiac.autoconfigure.lock.LockProperties;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.lock.base.LockFactory;
import org.zodiac.lock.database.DataSourceLockFactory;
import org.zodiac.lock.database.DataSourceLockHandler;
import javax.sql.DataSource;
@AutoConfigureBefore(value = {LockAutoConfiguration.class})
@ConditionalOnClass(value = {org.zodiac.lock.base.LockFactory.class})
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_LOCK_TYPE, havingValue = "database")
public class DatabaseLockAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(prefix = SystemPropertiesConstants.Zodiac.SPRING_LOCK_DATABASE_PREFIX,
ignoreInvalidFields = true)
protected DatabaseLockProperties databaseLockProperties() {
return new DatabaseLockProperties();
}
@SpringBootConfiguration
@ConditionalOnMissingBean(name = LockFactory.DATABASE_LOCK_FACTORY_BEAN)
@ConditionalOnBean(value = {DataSource.class})
protected static class LockFactoryAutoConfiguration {
@Bean
protected DataSourceLockHandler dataSourceLockHandler() {
return new DataSourceLockHandler();
}
@Bean(name = LockFactory.DATABASE_LOCK_FACTORY_BEAN)
public DataSourceLockFactory dataSourceLockFactory(DataSource dataSource,
DataSourceLockHandler dataSourceLockHandler, LockProperties lockProperties,
DatabaseLockProperties databaseLockProperties) {
return new DataSourceLockFactory(databaseLockProperties.getTableName(), dataSource, dataSourceLockHandler,
lockProperties.getScheduledPoolSize());
}
}
}