9、手写一个starter
一、starte详解:
1、starter场景启动器:
SpringBoot-starter是一个集成接合器,主要完成两件事:
(1)、引入模块所需的相关jar包
(2)、自动配置各自模块所需的属性
注:
spring-boot-starter-*:官方提供
*-spring-boot-starter:第三方提供
2、starter实现原理:
(1)、首先,SpringBoot在启动时会先去依赖的starter包中寻找 resources/META-INF/spring.factories 文件,然后根据文件中配置的Jar包去扫描项目所依赖的Jar包
(2)、然后,根据 spring.factories 配置加载自动配置类。
(3)、最后,根据 @Conditional 注解的条件,进行自动配置,并将Bean注入Spring Context上下文当中。
二、Springboot自动配置原理:
Springboot启动的时候会通过@EnableAutoConfiguration注解找到配置文件中的所有自动配置类(XxxxAutoConfiguration类),并对其进行加载,而这些自动配置类都是以AutoConfiguration结尾来命名的,它实际上就是一个JavaConfig形式的Spring容器配置类,它能通过以Properties结尾命名的类中取得在全局配置文件中配置的属性如:server.port,而XxxxProperties类是通过@ConfigurationProperties注解与全局配置文件中对应的属性进行绑定的。
三、手写一个starter:
手写一个starter。功能很简单,调用starter内对象的一个方法输出"xxx,hello! xxx"
1、hello-spring-boot-starter工程:
(1)、改POM:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.iven</groupId> <artifactId>hello-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hello-spring-boot-starter</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--SpringBoot框架web项目起步依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--自动读取配置元数据:--> <!--参考:https://www.jianshu.com/p/ca22783b0a35--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!--springBoot自动配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <!-- Lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> </dependencies> <!--打pom包的插件--> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> </project>
(2)、编写业务类Service:
@Data @AllArgsConstructor //全参构造 @NoArgsConstructor //无参构造 public class HelloService { private String name; private String detail; public String hello() { return getName()+",hello! " + getDetail(); } }
(3)、编写Configuration配置类(主):
@Configuration //表明这是一个springBoot的配置类 public class HelloConfig { @Value("${com.hello:Iven}") private String name; @Value("${com.hello:冲冲冲}") private String detail; @Bean //将对象放入IOC容器中,对象名就是方法名 public HelloService helloService(){ return new HelloService(name,detail); } }
(4)、编写spring.factories文件:
为了让springboot容器扫描到配置类,建一个resource目录,一个META-INF文件夹和spring.factories文件

#等号后面是配置类的全路径(包+配置类名) org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.iven.hellospringbootstarter.config.HelloConfig
(5)、install打包到本地仓库;
2、test-starter-demo工程:
(1)、添加依赖:
<dependency>
<groupId>com.iven</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
(2)、使用:
@RestController @RequestMapping("/v1") public class control { @Autowired private HelloService helloService; @RequestMapping("") public String demo(){ helloService.setName("Pitt"); return helloService.hello(); } }
浙公网安备 33010602011771号