k4243kian 8 months ago committed by cyj
commit 145dcb8036

@ -16,16 +16,29 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* AdminBeanConfigSpringBean便Spring使
* BeanID
*
* @author lanhai
*/
@Configuration
// @Configuration注解表明这个类是一个配置类Spring会扫描到这个类并根据其中定义的Bean配置方法来创建和管理相应的Bean对象。
@AllArgsConstructor
// @AllArgsConstructor注解会为这个类生成一个包含所有参数的构造函数方便依赖注入操作在这里用于注入AdminConfig实例。
public class AdminBeanConfig {
// 通过构造函数注入AdminConfig实例AdminConfig应该是包含了相关配置信息的类此处用于获取创建Snowflake实例所需的参数。
private final AdminConfig adminConfig;
/**
* snowflakeSpringSnowflakeBean
* SnowflakeID
* AdminConfigworkerIddatacenterIdSnowflakeID
*
* @return SnowflakeSpringID使
*/
@Bean
public Snowflake snowflake() {
return new Snowflake(adminConfig.getWorkerId(), adminConfig.getDatacenterId());
return new Snowflake(adminConfig.getWorkerId(), adminConfig.getDatacenterId());
}
}
}

@ -15,25 +15,36 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
*
* AdminConfig
* Spring便使
*
* @author lgh
*/
@Data
@Component
// @Component注解将该类标记为Spring组件意味着Spring容器在扫描时会识别这个类并将其纳入管理范围
// 这样就可以在其他需要使用该配置信息的地方通过依赖注入的方式获取到这个类的实例。
@PropertySource("classpath:admin.properties")
// @PropertySource注解用于指定配置属性的来源这里明确表示配置信息将从类路径下的admin.properties文件中获取。
@ConfigurationProperties(prefix = "admin")
// @ConfigurationProperties注解的作用是把配置文件中以"admin"为前缀的属性绑定到这个类对应的成员变量上,实现配置属性的自动注入。
@Data
// @Data是Lombok提供的注解它会自动为类生成一系列常用的方法包括各个成员变量的Getter和Setter方法、
// toString方法、equals方法以及hashCode方法等方便对类中的成员变量进行操作和访问。
public class AdminConfig {
/**
* ID
*/
private Integer datacenterId;
/**
* ID
* ID使Snowflake
* ID
*/
private Integer datacenterId;
/**
* ID
*/
private Integer workerId;
/**
* ID
* ID
* ID
*/
private Integer workerId;
}
}
Loading…
Cancel
Save