SpringBoot自定义starter

前情回顾

SpringBoot提供了一些自定义的start,比较常见的如spring-boot-starter-web,spring-boot-starter-thymeleaf,spring-boot-starter-tomcat,命名为“spring-boot-starter-xxx”,参考官网[1]
除此之外,还有很多第三方的start[2],比如druid-spring-boot-starter,命名为“xxx-spring-boot-starter”。

教程参考[3]

编写代码

配置类 XXXProperties.java

主要是通过该类在application.yml配置start的属性,通过前置prefix来区分配置

package com.atguigu.boot3.robot.properties;

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

/**
 * @author 朱俊伟
 * @date 2023/10/30 22:18
 */
@Data
@Component
@ConfigurationProperties(prefix = "robot")
public class RobotProperties {
    private String name;
    private String age;
    private String email;
}

service

package com.atguigu.boot3.robot.service;

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

/**
 * @author 朱俊伟
 * @date 2023/10/30 22:20
 */
@Service
public class RobotService {

    @Autowired
    RobotProperties robotProperties;

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

controller

package com.atguigu.boot3.robot.controller;

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

/**
 * @author 朱俊伟
 * @date 2023/10/30 22:20
 */
@RestController
public class RobotController {

    @Autowired
    RobotService robotService;

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

自动配置类

这个配置类比较重要,基本上start的被SpringBoot识别是通过这个类来进行IOC
给容器中导入Robot功能要用的所有组件

package com.atguigu.boot3.robot;

import com.atguigu.boot3.robot.controller.RobotController;
import com.atguigu.boot3.robot.properties.RobotProperties;
import com.atguigu.boot3.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 {

}

org.springframework.boot.autoconfigure.AutoConfiguration.imports配置文件

非常重要,定义哪些类被SpringBoot加载

/META-INF/org.springframework.boot.autoconfigure.AutoConfiguration.imports

其他地方使用

直接导入xxx-spring-boot-starter
该项目不规范,忽略。。

<dependency>
            <groupId>com.atguigu</groupId>
            <artifactId>boot3-08-robot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

同时在application.yml添加对应配置即可

robot:
  age: 20
  name: zjw

测试

http://localhost:8080/robot/hello

参考:


  1. https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters ↩︎

  2. https://mvnrepository.com/tags/starter ↩︎

  3. https://www.yuque.com/leifengyang/springboot3/lliphvul8b19pqxp#fR0xi ↩︎

posted @ 2023-10-31 00:19  雨中遐想  阅读(46)  评论(0编辑  收藏  举报