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();
    }
}
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on   爱文(Iven)  阅读(185)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示

目录导航