在Spring Boot中自写插件或封包(通常指的是创建自定义的starter)是一种常见的做法,用于将一系列相关的配置和组件打包成一个独立的模块,从而简化依赖管理和配置过程。以下是一个简单的步骤,指导你如何创建一个自定义的Spring Boot Starter:
使用你喜欢的构建工具(如Maven或Gradle)创建一个新的项目。
在你的pom.xml
或build.gradle
文件中,添加Spring Boot Starter的依赖。例如,使用Maven的话,你可以这样添加依赖:
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- <version>${spring-boot.version}</version>
- </dependency>
- <!-- 其他依赖 -->
- </dependencies>
xml复制代码
创建一个自动配置类,使用@Configuration
和@EnableConfigurationProperties
注解。这个类将负责配置你的插件。
- @Configuration
- @EnableConfigurationProperties(MyPluginProperties.class)
- public class MyPluginAutoConfiguration {
-
- @Autowired
- private MyPluginProperties properties;
-
- @Bean
- public MyPluginService myPluginService() {
- return new MyPluginServiceImpl(properties);
- }
- }
创建一个配置属性类,使用@ConfigurationProperties
注解,定义你的插件需要的配置。
- @ConfigurationProperties(prefix = "my-plugin")
- public class MyPluginProperties {
-
- private String someProperty;
-
- // getters and setters
- }
创建一个服务类,实现你的插件功能。
- public class MyPluginServiceImpl implements MyPluginService {
-
- private final MyPluginProperties properties;
-
- public MyPluginServiceImpl(MyPluginProperties properties) {
- this.properties = properties;
- }
-
- // 实现你的插件功能
- }
src/main/resources/META-INF/spring.factories
中注册自动配置类创建一个spring.factories
文件,在src/main/resources/META-INF/
目录下 springboot3.x以后用org.springframework.boot.autoconfigure.AutoConfiguration.imports文件代替,
并添加以下内容:
spring.factories springboot2.x
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- com.yourpackage.MyPluginAutoConfiguration
org.springframework.boot.autoconfigure.AutoConfiguration.imports springboot3.x
com.yourpackage.MyPluginAutoConfiguration
使用构建工具打包你的项目,并发布到你的Maven仓库或其他仓库。
在其他Spring Boot项目中,添加你发布的starter作为依赖,然后在application.properties
或application.yml
中配置你的插件。
my-plugin.some-property=some-value
最后,你可以在项目中使用@Autowired
注解注入你的服务类,并开始使用你的插件功能。
这就是创建一个简单的Spring Boot Starter的基本步骤。你可以根据你的需求进行扩展和定制。
部分代码示例
ZxsAutoConfig
- package com.zxs.sso.config;
-
-
- import com.zxs.sso.core.util.JedisUtil;
- import com.zxs.sso.properties.ZxsProperties;
- import jakarta.annotation.Resource;
- import org.springframework.beans.factory.DisposableBean;
- import org.springframework.boot.context.properties.EnableConfigurationProperties;
- import org.springframework.boot.web.servlet.FilterRegistrationBean;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- @EnableConfigurationProperties(ZxsProperties.class)
- @ComponentScan("com.zxs")
- public class ZxsAutoConfig implements DisposableBean {
-
- @Resource
- ZxsProperties zxsProperties;
-
- @Bean
- public FilterRegistrationBean xxlSsoFilterRegistration() {
- JedisUtil.init(zxsProperties.getRedisAddress());
- FilterRegistrationBean registration = new FilterRegistrationBean();
-
- //逻辑代码
- return registration;
- }
-
- @Override
- public void destroy() throws Exception {
-
- }
- }
ZxsProperties
- package com.zxs.sso.properties;
-
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
-
- @Component
- @ConfigurationProperties(prefix = "zxs")
- public class ZxsProperties {
-
- private String zxsServer;
- private String logout;
- private String excludeds;
- private String redisAddress;
- private Integer expire;
- private Boolean isToken;
-
- public String getZxsServer() {
- return zxsServer;
- }
-
- public void setZxsServer(String zxsServer) {
- this.zxsServer = zxsServer;
- }
-
- public String getLogout() {
- return logout;
- }
-
- public void setLogout(String logout) {
- this.logout = logout;
- }
-
- public String getExcludeds() {
- return excludeds;
- }
-
- public void setExcludeds(String excludeds) {
- this.excludeds = excludeds;
- }
-
- public String getRedisAddress() {
- return redisAddress;
- }
-
- public void setRedisAddress(String redisAddress) {
- this.redisAddress = redisAddress;
- }
-
- public Integer getExpire() {
- return expire;
- }
-
- public void setExpire(Integer expire) {
- this.expire = expire;
- }
-
- public Boolean getToken() {
- return isToken;
- }
-
- public void setToken(Boolean token) {
- isToken = token;
- }
- }
additional-spring-configuration-metadata.json
- {
- "properties": [
- {
- "name": "zxs.zxsServer",
- "description": "SSO服务地址.",
- "defaultValue": "http://127.0.0.1:8080/zxs-server",
- "type": "java.lang.String"
- },
- {
- "name": "zxs.login",
- "type": "java.lang.String"
- },
- {
- "name": "zxs.logout",
- "description": "登出路径.",
- "type": "java.lang.String"
- },
- {
- "name": "zxs.excludeds",
- "description": "排除路径.",
- "type": "java.lang.String"
- },
- {
- "name": "zxs.redisAddress",
- "description": "redis地址.",
- "defaultValue": "redis://zxs:zkb123456@127.0.0.1:6379/0",
- "type": "java.lang.String"
- },
- {
- "name": "zxs.isToken",
- "description": "授权类型.",
- "defaultValue": false,
- "type": "java.lang.Boolean"
- },
- {
- "name": "zxs.expire",
- "description": "时长.",
- "defaultValue": 3600,
- "type": "java.lang.Integer"
- }
- ]
- }
org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.zxs.sso.config.ZxsAutoConfig