我们可以看到,这个文件比较简单就引入了:
- spring-boot-starter:springboot的相关jar包 。
- spring-boot-autoconfigure:springboot自动配置相关jar包 。
- spring-boot-configuration-processor:springboot生成IDE提示功能相关jar包 。
spring.factories
文件:org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.sue.IdGenerateAutoConfiguration
它里面只包含一行配置,其中key是EnableAutoConfiguration,value是IdGenerateAutoConfiguration 。再重点看一下IdGenerateAutoConfiguration
@ConditionalOnClass(IdProperties.class)@EnableConfigurationProperties(IdProperties.class)@Configurationpublic class IdGenerateAutoConfiguration {@Autowiredprivate IdProperties properties;@Beanpublic IdGenerateService idGenerateService() {return new IdGenerateService(properties.getWorkId());}}
该类是一个使用了@Configuration
注解标记为了配置类,生效的条件是@ConditionalOnClass
注解中检测到包含IdProperties.class 。并且使用@EnableConfigurationProperties
注解会自动注入IdProperties的实例 。此外,最关键的点是该类里面创建了idGenerateService的bean实例,这是自动配置的精髓 。
再看看IdGenerateService
public class IdGenerateService {private Long workId;public IdGenerateService(Long workId) {this.workId = workId;}public Long generate() {return new Random().nextInt(100) + this.workId;}}
我们可以看到它是一个普通的类,甚至都没有使用@Service
注解,里面有个generate方法,根据workId的值和随机数动态生成id 。最后看看IdProperties
@ConfigurationProperties(prefix = IdProperties.PREFIX)public class IdProperties {public static final String PREFIX = "sue";private Long workId;public Long getWorkId() {return workId;}public void setWorkId(Long workId) {this.workId = workId;}}
它是一个配置实体类,里面包含了相关的配置文件 。使用@ConfigurationProperties
注解,会自动把application.properties
文件中以sue开通的,参数名称跟IdProperties中一样的参数值,自动注入到IdProperties对象中 。3.3 创建id-generate-test【SpringBoot的starter到底是什么?】这个项目主要用于测试 。

文章插图
该项目里面包含:pom.xml、application.properties、Application 和 TestRunner 文件 。
先看看pom.xml文件
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><version>1.3.1</version><groupId>com.sue</groupId><artifactId>spring-boot-id-generate-test</artifactId><name>spring-boot-id-generate-test</name><dependencies><dependency><groupId>com.sue</groupId><artifactId>id-generate-spring-boot-starter</artifactId><version>1.3.1</version></dependency></dependencies></project>
由于只测试刚刚定义的id生成功能,所以只引入的id-generate-spring-boot-starter jar包 。application.properties配置资源文件
sue.workId=123
只有一行配置,因为我们的IdProperties中目前只需要这一个参数 。Application是测试程序启动类
@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
很简单,就是一个普通的springboot启动类TestRunner是我们的测试类
@Componentpublic class TestRunner implements ApplicationRunner {@Autowiredprivate IdGenerateService idGenerateService;public void run(ApplicationArguments args) throws Exception {Long sysNo = idGenerateService.generate();System.out.println(sysNo);}}
经验总结扩展阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 比较高级的土味情话
- 大麦小麦和燕麦的区别
- 2023年2月15日是请佛吉日吗 2023年2月15日是请佛的黄道吉日吗
- 钨做灯丝的原因
- 雄蚕蛾泡酒方法
- 微信运动关注的人是什么意思
- 通大便的食物与水果
- 经典力学的奠基人是谁
- 中度贫血会头晕吗
- 养生堂维生素c的功效