场景:抽取聊天机器人场景,它可以打招呼。
效果:任何项目导入此starter
都具有打招呼功能,并且问候语中的人名需要可以在配置文件中修改
● 1. 创建自定义 starter
项目,引入 spring-boot-starter
基础依赖
● 2. 编写模块功能,引入模块所有需要的依赖。
● 3. 编写 xxxAutoConfiguration
自动配置类,帮其他项目导入这个模块需要的所有组件
● 4. 编写配置文件 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
指定启动需要加载的自动配置
● 5. 其他项目引入即可使用
新建项目-boot3-08-robot-starter
新建模块
boot3-08-robot-starter
创建模块
项 | 值 |
---|---|
名称 | boot3-08-robot-starter |
位置 | D:\Study-Java\2023\SpringBoot3-Study |
语言 | Java |
构建系统 | Maven |
JDK | 版本:17 供应商:Eclipse Temurin(AdoptOpenJDK HotSpot)17.0.8 位置:C:\Users\Administrator.jdks\temurin-17.0.8 |
主 ID | com.atguigu |
工件 ID | boot3-08-robot-starter |
软件包名称 | com.atguigu.boot3.starter |
- SpringBoot
3.2.0
- 依赖项目
Developer Tools
-- Lombok
Web
-- Spring Web
目录结构
│ pom.xml
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─atguigu
│ │ │ └─boot3
│ │ │ └─starter
│ │ │ ├─Controller
│ │ │ │ RobotController.java
│ │ │ │
│ │ │ └─robot
│ │ │ EnableRobot.java
│ │ │ RobotAutoConfiguration.java
│ │ │ RobotProperties.java
│ │ │ RobotService.java
│ │ │
│ │ └─resources
│ │ └─META-INF
│ │ └─spring
│ │ org.springframework.boot.autoconfigure.AutoConfiguration.imports
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 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>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.atguigu</groupId>
<artifactId>boot3-08-robot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot3-08-robot-starter</name>
<description>boot3-08-robot-starter</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 导入配置处理器,配置文件自定义的 properties 配置都会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
属性类-RobotProperties
package com.atguigu.boot3.starter.robot;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 属性类
*/
@Data
@Component
@ConfigurationProperties(prefix = "robot")
public class RobotProperties {
private String name;
private String age;
private String email;
}
服务类-RobotService
package com.atguigu.boot3.starter.robot;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class RobotService {
@Resource
RobotProperties robotProperties;
public String sayHello() {
return String.format("姓名:%s,年龄:%s,邮箱:%s, Hello!",
robotProperties.getName(),robotProperties.getAge(),robotProperties.getEmail());
}
}
控制器-RobotController
package com.atguigu.boot3.starter.Controller;
import com.atguigu.boot3.starter.robot.RobotService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RobotController {
@Resource
RobotService robotService;
@GetMapping("/robot/hello")
public String sayHello() {
return robotService.sayHello();
}
}
修改项目-boot3-06-features
项目结构
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─atguigu
│ │ │ └─boot306
│ │ │ └─features
│ │ │ │ Application.java
│ │ │
│ │ └─resources
│ │ │ application.properties
pom.xml 引入 boot3-08-robot-starter
<!--自定义 starter 项目-->
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>boot3-08-robot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
修改配置文件-application.properties
robot.name=鲁班一号
robot.age=1
robot.email=luban1@qq.com
第一种方式-使用 @Import 导入自定义 starter
- 自己写一个
RobotAutoConfiguration
,给容器中导入这个场景需要的所有组件- 为什么这些组件默认不会扫描进去?
starter
所在的包和引入它的项目的主程序所在的包不是父子层级
- 别人引用这个
starter
,直接导入这个RobotAutoConfiguration
,就能把这个场景的组件导入进来 - 功能生效。
boot3-08-robot-starter 项目新增 RobotAutoConfiguration
package com.atguigu.boot3.starter.robot;
import com.atguigu.boot3.starter.Controller.RobotController;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* 给容器中导入 Robot 功能要用的所有组件的配置类
*/
@Configuration
@Import({RobotProperties.class, RobotService.class, RobotController.class})
public class RobotAutoConfiguration {
}
boot3-06-features 项目主启动类添加 @Import 注解
@Slf4j
@SpringBootApplication //主程序类
//使用导入方式——导入自定义 starter 所有组件
@Import(RobotAutoConfiguration.class)
public class Application
测试
启动项目
boot3-06-features
返回
姓名:鲁班一号,年龄:1,邮箱:luban1@qq.com, Hello!
第二种方式-使用 @EnableXxx 机制导入自定义 starter
boot3-08-robot-starter 项目新增 EnableRobot
package com.atguigu.boot3.starter.robot;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* 导入 RobotAutoConfiguration 的注解
*/
@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import(RobotAutoConfiguration.class)
public @interface EnableRobot {
}
boot3-06-features 项目主启动类添加 @EnableRobot 注解
别人引入 starter
需要使用 @EnableRobot
开启功能
@Slf4j
@SpringBootApplication //主程序类
//使用导入方式——导入自定义 starter 所有组件
//@Import(RobotAutoConfiguration.class)
//使用@EnableXxx机制——导入自定义 starter 所有组件
@EnableRobot
public class Application
测试
启动项目
boot3-06-features
返回
姓名:鲁班一号,年龄:1,邮箱:luban1@qq.com, Hello!
第三种方式-完全自动配置
● 依赖 SpringBoot
的 SPI
机制
● META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
文件中编写好我们自动配置类的全类名即可
● 项目启动,自动加载我们的自动配置类
boot3-08-robot-starter 项目新增资源目录配置
resources
下新增文件META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.atguigu.boot3.starter.robot.RobotAutoConfiguration
boot3-06-features 项目主启动类去掉所有相关注解
@Slf4j
@SpringBootApplication //主程序类
//使用导入方式——导入自定义 starter 所有组件
//@Import(RobotAutoConfiguration.class)
//使用@EnableXxx机制——导入自定义 starter 所有组件
//@EnableRobot
public class Application
测试
启动项目
boot3-06-features
返回
姓名:鲁班一号,年龄:1,邮箱:luban1@qq.com, Hello!