com.feingto.cloud.config.idworker.IdGeneratorAutoConfiguration Maven / Gradle / Ivy
The newest version!
package com.feingto.cloud.config.idworker;
import com.feingto.cloud.kit.Identities;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* ID 生成器自动注入配置
*
* @author longfei
*/
@Slf4j
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(SnowflakeIdWorkerProperties.class)
public class IdGeneratorAutoConfiguration {
public static final String ID_BEAN_NAME = "idGenerator";
@Bean(ID_BEAN_NAME)
@ConditionalOnProperty(prefix = SnowflakeIdWorkerProperties.PREFIX, name = "worker-id")
public IdGenerator snowflakeIdGenerator(SnowflakeIdWorkerProperties properties) {
log.info("Bean '{}' has been injected", SnowflakeIdGenerator.class.getName());
return new SnowflakeIdGenerator(new SnowflakeIdWorker(properties.getWorkerId(), properties.getDatacenterId()));
}
@Bean(ID_BEAN_NAME)
@ConditionalOnMissingBean
public IdGenerator idGenerator() {
log.info("Bean '{}' has been injected", IdGenerator.class.getName());
return Identities::uuid;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy