自定义Spring Boot Starter

1.引入依赖

 <packaging>jar</packaging>
    <dependencies>
            <!-- Spring Boot 自动配置依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-autoconfigure</artifactId>
                <version>2.6.13</version>
            </dependency>
            <!-- 可选:配置注解处理器 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <version>2.6.13</version>
                <optional>true</optional>
            </dependency>
    </dependencies>

2.配置属性类

package top.remained.lx.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author lx
 * @date 2025/5/8
 * @description 配置属性类
 */
@ConfigurationProperties(prefix = "lx")
public class LxProperties {
    private String name = "default";
    private int timeout = 1000;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
}

3.业务服务类

package top.remained.lx.service;

/**
 * @author lx
 * @date 2025/5/8
 * @description 业务服务类
 */
public class LxService {
    private final String name;
    private final int timeout;

    public LxService(String name, int timeout) {
        this.name = name;
        this.timeout = timeout;
    }

    public String greet() {
        return "Hello, " + name + "! (timeout: " + timeout + "ms)";
    }
}

4.创建自动配置类

package top.remained.lx.configuration;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.remained.lx.properties.LxProperties;
import top.remained.lx.service.LxService;

/**
 * @author lx
 * @date 2025/5/8
 * @description
 */
@Configuration
@EnableConfigurationProperties(LxProperties.class)
@ConditionalOnClass(LxService.class)
@ConditionalOnProperty(prefix = "lx", name = "enabled",havingValue = "true",matchIfMissing = true)
public class LxConfiguration {

    @Bean
//    @ConditionalOnMissingBean(LxProperties.class) // 如果没有创建bean 则创建默认bean
    public LxService lxService(LxProperties lxProperties) {
        return new LxService(lxProperties.getName(), lxProperties.getTimeout());
    }
}

5.注册自动配置

在resources/META-INF下创建spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=top.remained.lx.configuration.LxConfiguration

6.案例使用

6.1 引入依赖

      <dependency>
            <groupId>top.remained</groupId>
            <artifactId>lx-stater</artifactId>
            <version>2.7.8</version>
        </dependency>

6.2 手动引入jar包

mvn install:install-file -D"file=C:\soft\fastexcel-1.1.0.jar" -D"groupId=cn.idev.excel" -D"artifactId=fastexcel" -D"version=1.1.0" -Dpackaging=jar

6.3 yml中配置

在这里插入图片描述

6.4 实际使用

@RequestMapping("/lx")
@RestController
public class TestLXStarterController {
    @Autowired
    private LxService lxService;
    @GetMapping("/greet")
    public String greet() {
        return lxService.greet();
    }
}

7.包结构

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值