解決自定義的springboot starter包的問題

spring boot 3.x 版本,使用META-INF/spring路徑,指定配置文件,org.springframework.boot.autoconfigure.AutoConfiguration.imports來代替了Spring boot 2.x版本的spring.factories模式。3.x以上,已經(jīng)采用@AutoConfiguration注解來實(shí)現(xiàn)了,不需要再去手動添加,但是在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目錄下創(chuàng)建 spring.factories文件:`

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

以下custom-starter 實(shí)現(xiàn)示例:


image.png
  • 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工程項(xiàng)目,關(guān)鍵是添加:

  <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就可以了,關(guān)鍵代碼
@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!

最終效果展示:


image.png

關(guān)鍵點(diǎn):
關(guān)鍵點(diǎn):

  • 通過@AutoConfiguration標(biāo)記為自動配置類,
  • 在模塊A的META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中聲明該類。
    以上兩種設(shè)置,模塊B引用模塊A后,無需手動導(dǎo)入,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();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容