Class SnowflakeAutoConfiguration
java.lang.Object
com.fayupable.snowflake.jpaspring.SnowflakeAutoConfiguration
@Configuration
@EnableConfigurationProperties(SnowflakeProperties.class)
public class SnowflakeAutoConfiguration
extends Object
Wires a Spring-managed
IdGenerator from SnowflakeProperties and
publishes it to SnowflakeIdGeneratorHolder, since Hibernate instantiates
SnowflakeIdentifierGenerator via reflection and never asks the
ApplicationContext for it. This class is the one place that bridges the two.
Registered automatically through
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports,
the same mechanism every official Spring Boot starter uses. Consumers only need to
add the snowflake-jpa-spring dependency and set snowflake.datacenter-id
and snowflake.worker-id; no @Import or other manual wiring is required.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionidGenerator(SnowflakeConfig config, org.springframework.beans.factory.ObjectProvider<io.micrometer.core.instrument.MeterRegistry> registryProvider) Creates the singletonIdGeneratorfor this application instance and registers it withSnowflakeIdGeneratorHolderso that@SnowflakeGeneratedId-annotated entity fields can be populated by Hibernate.snowflakeConfig(SnowflakeProperties properties) Builds theSnowflakeConfigshared by theidGenerator(com.fayupable.snowflake.config.SnowflakeConfig, org.springframework.beans.factory.ObjectProvider<io.micrometer.core.instrument.MeterRegistry>)andsnowflakeHealthIndicator(com.fayupable.snowflake.config.SnowflakeConfig)beans, so both read the same datacenter/worker/epoch values from a single source.org.springframework.boot.health.contributor.HealthIndicatorExposesdatacenterId,workerIdand the timestamp overflow horizon through/actuator/health, so operators can check this instance's Snowflake identity and epoch headroom without reading logs.
-
Constructor Details
-
SnowflakeAutoConfiguration
public SnowflakeAutoConfiguration()
-
-
Method Details
-
snowflakeConfig
Builds theSnowflakeConfigshared by theidGenerator(com.fayupable.snowflake.config.SnowflakeConfig, org.springframework.beans.factory.ObjectProvider<io.micrometer.core.instrument.MeterRegistry>)andsnowflakeHealthIndicator(com.fayupable.snowflake.config.SnowflakeConfig)beans, so both read the same datacenter/worker/epoch values from a single source.- Parameters:
properties- the boundsnowflake.*configuration, providingdatacenterId,workerIdandepoch- Returns:
- the layout and identity configuration for this application instance
-
idGenerator
@Bean public IdGenerator idGenerator(SnowflakeConfig config, org.springframework.beans.factory.ObjectProvider<io.micrometer.core.instrument.MeterRegistry> registryProvider) Creates the singletonIdGeneratorfor this application instance and registers it withSnowflakeIdGeneratorHolderso that@SnowflakeGeneratedId-annotated entity fields can be populated by Hibernate.- Parameters:
config- the sharedSnowflakeConfigfor this instanceregistryProvider- the application'sMeterRegistry, if one exists; when absent, generator activity is simply not measured, no metrics backend is required- Returns:
- a thread-safe
IdGeneratorbacked by the real system clock, exposed as a regular Spring bean in case other components want to generate ids directly (e.g. for non-JPA use cases) via normal dependency injection instead of the static holder
-
snowflakeHealthIndicator
@Bean @ConditionalOnClass(org.springframework.boot.health.contributor.HealthIndicator.class) public org.springframework.boot.health.contributor.HealthIndicator snowflakeHealthIndicator(SnowflakeConfig config) ExposesdatacenterId,workerIdand the timestamp overflow horizon through/actuator/health, so operators can check this instance's Snowflake identity and epoch headroom without reading logs. Only registered when Spring Boot Actuator is on the classpath.- Parameters:
config- the sharedSnowflakeConfigfor this instance- Returns:
- a health indicator reporting static identity and epoch details
-