SpringBoot自定义Starter(@EnableXXX和META-INF的SPI自动添加)

目录
1. 自定义Starter
1.1 场景和效果
1.2 starter实现
1.2.1 创建自定义starter项目
1.2.2 把所有maven依赖导入
1.2.3 实现公共代码逻辑
1.2.4 添加方式一:实现RobotAutoConfiguration配置类
1.2.5 添加方式二:实现RobotAutoConfiguration配置类 + @EnableRobot功能开关
1.2.6 添加方式三:实现RobotAutoConfiguration配置类 + META-INF的imports文件
1.2.7 打包到本地仓库
1.3 项目使用
1.3.1 把starter的maven依赖导入
1.3.2 application.properties添加配置参数
1.3.3 注入方式一:使用@Import
1.3.4 注入方式二:使用@EnableRobot
1.3.5 注入方式二:自动注入
1.3.6 功能测试
1. 自定义Starter
1.1 场景和效果
场景:抽取聊天机器人场景,它可以打招呼
效果:任何项目导入此starter都具有打招呼功能,并且可以在配置文件中修改问候语中的人名
1.2 starter实现
1.2.1 创建自定义starter项目


1.2.2 把所有maven依赖导入
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <skip>true</skip>    <!-- 这样没有main主程序类也可以打包 -->
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
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
32
33
34
35
36
1.2.3 实现公共代码逻辑
删除RobotSpringBootStarterApplication主程序类,删除src/test目录

RobotProperties.java:绑定application.properties中robot开头的配置到属性name和age

package com.hh.robot.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "robot")
@Component
@Data
public class RobotProperties {
    private String name;
    private String age;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
RobotService.java:从robotProperties获取属性name和age
package com.hh.robot.service;

import com.hh.robot.properties.RobotProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class RobotService {

    @Autowired
    RobotProperties robotProperties;

    public String sayHello() {
        return "你好:" +
                "名字:【" + robotProperties.getName() + "】, " +
                "年龄:【" + robotProperties.getAge() + "】";
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
RobotController.java:接收请求,返回robot的问候语
package com.hh.robot.controller;

import com.hh.robot.service.RobotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class RobotController {

    @Autowired
    RobotService robotService;

    @GetMapping("/robot")
    public String sayHello() {
        String hello = robotService.sayHello();
        return hello;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1.2.4 添加方式一:实现RobotAutoConfiguration配置类
实现RobotAutoConfiguration配置类,给IOC容器导入Robot功能要用的所有组件。如下所示

package com.hh.robot.config;

import com.hh.robot.controller.RobotController;
import com.hh.robot.properties.RobotProperties;
import com.hh.robot.service.RobotService;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;


@Import({RobotController.class, RobotProperties.class, RobotService.class})
@Configuration
public class RobotAutoConfiguration {


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1.2.5 添加方式二:实现RobotAutoConfiguration配置类 + @EnableRobot功能开关
参照添加方式一实现RobotAutoConfiguration配置类。再实现@EnableRobot功能开关,添加RobotAutoConfiguration配置类。如下所示:

package com.hh.robot.annotation;

import com.hh.robot.config.RobotAutoConfiguration;
import org.springframework.context.annotation.Import;

import java.lang.annotation.*;


@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import(RobotAutoConfiguration.class)
public @interface EnableRobot {


}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1.2.6 添加方式三:实现RobotAutoConfiguration配置类 + META-INF的imports文件
参照添加方式一实现RobotAutoConfiguration配置类。再在src\main\resources\META-INF\spring\org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中,添加RobotAutoConfiguration配置类的全类名。文件内容如下:

com.hh.robot.config.RobotAutoConfiguration
1
1.2.7 打包到本地仓库
使用maven命令mvn clean install进行打包到本地仓库

1.3 项目使用
1.3.1 把starter的maven依赖导入
        <dependency>
            <groupId>com.hh</groupId>
            <artifactId>robot-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
1
2
3
4
5
1.3.2 application.properties添加配置参数
application.properties添加配置参数如下:

robot.name=jim
robot.age=18
1
2
1.3.3 注入方式一:使用@Import
使用@Import注解,将RobotAutoConfiguration注入到IOC容器,就会自动将robot项目的所有组件注册到IOC容器。如下所示:

......省略部分......
@SpringBootApplication
@Import({RobotAutoConfiguration.class})
public class SpringBootTestApplication {
......省略部分......
}
1
2
3
4
5
6
1.3.4 注入方式二:使用@EnableRobot
使用@EnableRobot注解,自动将RobotAutoConfiguration注入到IOC容器,就会自动将robot项目的所有组件注册到IOC容器。如下所示:

......省略部分......
@SpringBootApplication
@EnableRobot
public class SpringBootTestApplication {
......省略部分......
}
1
2
3
4
5
6
1.3.5 注入方式二:自动注入
基本原理:springboot启动的时候,会自动从robot-spring-boot-starter-0.0.1-SNAPSHOT.jar的src\main\resources\META-INF\spring\org.springframework.boot.autoconfigure.AutoConfiguration.imports,找到RobotAutoConfiguration配置类,将RobotAutoConfiguration注入到IOC容器,就会自动将robot项目的所有组件注册到IOC容器

1.3.6 功能测试
访问http://localhost:8080/robot,页面效果如下:

————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/yy8623977/article/details/131918396

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值