解决自定义的springboot starter包的问题

spring boot 3.x 版本,使用META-INF/spring路径,指定配置文件,org.springframework.boot.autoconfigure.AutoConfiguration.imports来代替了Spring boot 2.x版本的spring.factories模式。3.x以上,已经采用@AutoConfiguration注解来实现了,不需要再去手动添加,但是在3.x以上的版本,如果还使用@Configuration的,就要这么配置:

src/main/resources/META-INF/spring
org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.wewetea.open.CustomAutoConfiguration
com.example.customstarter.xxxxAutoConfiguration
.....

兼容2的版本,要这么配置:
在 src/main/resources/META-INF目录下创建 spring.factories文件:`

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.customstarter.CustomAutoConfiguration \
com.example.customstarter.xxxxAutoConfiguration
.....

以下custom-starter 实现示例:


  • CustomService.java
package com.wewetea.open;

public class CustomService {
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public void doSomething() {
        System.out.println(message);
        System.out.println("=================================");
    }
}
  • CustomProperties.java
package com.wewetea.open;

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

@ConfigurationProperties(prefix = "custom")
public class CustomProperties {
    private String message = "Hello, Custom Starter!";

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

  • CustomAutoConfiguration.java
package com.wewetea.open;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(CustomProperties.class)
public class CustomAutoConfiguration {

    private final CustomProperties properties;

    public CustomAutoConfiguration(CustomProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean
    public CustomService customService() {
        CustomService service = new CustomService();
        service.setMessage(properties.getMessage());
        return service;
    }
}

  • pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.wewetea.open</groupId>
  <artifactId>custom-starter</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>custom-starter</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>3.4.5</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <version>3.4.5</version>
    <optional>true</optional>
  </dependency>
  </dependencies>
</project>

注意:这里建的是纯maven工程项目,关键是添加:

  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>3.4.5</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <version>3.4.5</version>
    <optional>true</optional>
  </dependency>

然后:mvn clean install就可以了。
测试工程,就比较简单了,直接使用Autowired就可以了,关键代码
@Autowired private CustomService customService;
完整示例:

package com.wewetea.open.mytest;

import com.wewetea.open.CustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MytestApplication implements CommandLineRunner {

    @Autowired
    private CustomService customService;

    public static void main(String[] args) {
        SpringApplication.run(MytestApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        customService.doSomething();
    }
}

配置文件:
application.properties配置一下,

custom.message=Welcome to the Custom Starter!

最终效果展示:



关键点:
关键点:

  • 通过@AutoConfiguration标记为自动配置类,
  • 在模块A的META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中声明该类。
    以上两种设置,模块B引用模块A后,无需手动导入,Spring Boot会自动加载。

或其它解决方案:通过:@Import(CustomService.class)

package com.wewetea.open.mytest;

import com.wewetea.open.CustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@Import(CustomService.class)
public class MytestApplication implements CommandLineRunner {

    @Autowired
    private CustomService customService;

    public static void main(String[] args) {
        SpringApplication.run(MytestApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        customService.doSomething();
    }
}
posted @ 2025-05-18 03:53  刘文江  阅读(96)  评论(0)    收藏  举报  来源