自定义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();
}
}