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

1. 自定义Starter

1.1 场景和效果

  • 场景:抽取聊天机器人场景,它可以打招呼
  • 效果:任何项目导入此starter都具有打招呼功能,并且可以在配置文件中修改问候语中的人名

1.2 starter实现

1.2.1 创建自定义starter项目

自定义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 实现公共代码逻辑

  1. 删除RobotSpringBootStarterApplication主程序类,删除src/test目录

  2. 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. 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. 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.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.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.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.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.3.2 application.properties添加配置参数

application.properties添加配置参数如下:

robot.name=jim
robot.age=18

1.3.3 注入方式一:使用@Import

使用@Import注解,将RobotAutoConfiguration注入到IOC容器,就会自动将robot项目的所有组件注册到IOC容器。如下所示:

......省略部分......
@SpringBootApplication
@Import({RobotAutoConfiguration.class})
public class SpringBootTestApplication {
......省略部分......
}

1.3.4 注入方式二:使用@EnableRobot

使用@EnableRobot注解,自动将RobotAutoConfiguration注入到IOC容器,就会自动将robot项目的所有组件注册到IOC容器。如下所示:

......省略部分......
@SpringBootApplication
@EnableRobot
public class SpringBootTestApplication {
......省略部分......
}

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,页面效果如下:
功能测试

  • 25
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Spring Boot自定义Starter的示例,该Starter实现了一个自定义的HelloWorld功能: 1. 创建Maven项目 首先,我们需要创建一个Maven项目作为我们自定义Starter的项目。在项目的pom.xml添加Spring Boot的依赖,以及其他需要集成的依赖。 2. 编写自动配置 在src/main/java目录下创建一个名为HelloWorldAutoConfiguration的,该用于自动配置HelloWorld功能: ```java @Configuration @ConditionalOnClass(HelloWorldService.class) @EnableConfigurationProperties(HelloWorldProperties.class) public class HelloWorldAutoConfiguration { @Autowired private HelloWorldProperties properties; @Bean @ConditionalOnMissingBean public HelloWorldService helloWorldService() { HelloWorldService service = new HelloWorldService(); service.setMsg(properties.getMsg()); return service; } @ConfigurationProperties(prefix = "hello.world") public static class HelloWorldProperties { private String msg = "Hello, world!"; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } } } ``` 上述代码,@Configuration注解表示该是一个配置,@ConditionalOnClass注解表示只有当HelloWorldService存在时才进行配置,@EnableConfigurationProperties注解表示将HelloWorldProperties注入到Spring容器。在helloWorldService方法,我们通过读取HelloWorldProperties配置来创建一个HelloWorldService实例。 3. 编写Starter 在src/main/java目录下创建一个名为HelloWorldStarter,该用于将自动配置注入到Spring容器: ```java @Configuration @EnableConfigurationProperties(HelloWorldProperties.class) @Import(HelloWorldAutoConfiguration.class) public class HelloWorldStarter { } ``` 上述代码,@Configuration注解表示该是一个配置,@EnableConfigurationProperties注解表示将HelloWorldProperties注入到Spring容器,@Import注解表示将HelloWorldAutoConfiguration注入到Spring容器。 4. 打包和发布Starter 在命令行运行以下命令,将自定义Starter打包成jar包: ``` mvn clean package ``` 然后将jar包发布到Maven仓库。 5. 在项目使用自定义Starter 在其他Spring Boot项目的pom.xml文件添加以下依赖: ```xml <dependency> <groupId>com.example</groupId> <artifactId>hello-world-starter</artifactId> <version>1.0.0</version> </dependency> ``` 在项目使用以下代码来测试自定义Starter是否生效: ```java @RestController public class TestController { @Autowired private HelloWorldService helloWorldService; @GetMapping("/hello") public String hello() { return helloWorldService.sayHello(); } } ``` 上述代码,我们通过@Autowired注解注入了HelloWorldService实例,并在hello方法调用了sayHello方法来测试自定义Starter是否生效。 以上就是Spring Boot自定义Starter的一个简单示例,通过自定义Starter,我们可以将自己的功能快速集成到Spring Boot,提高开发效率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值