欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot多模塊自動配置失效問題的解決方案

 更新時間:2025年05月22日 08:59:39   作者:李少兄  
在Spring?Boot多模塊項目中,模塊間配置不生效是一個復(fù)雜但可解決的問題,尤其涉及自動配置類、依賴沖突、條件注解以及IDE配置,所以本文給大家介紹了SpringBoot多模塊自動配置失效問題的解決方案,需要的朋友可以參考下

一、問題背景與場景

1.1 場景描述

假設(shè)存在兩個模塊:

  • 模塊A:提供通用配置(如跨域配置、全局異常處理、攔截器)。
  • 模塊B:引用模塊A,但模塊A的配置未生效(如跨域配置無效、異常處理器未捕獲異常)。

1.2 核心問題

  1. 自動配置類未被加載:模塊A的@AutoConfiguration類未在模塊B中生效。
  2. 依賴沖突:第三方庫間接引入了與模塊A沖突的依賴(如日志框架版本不一致)。
  3. 條件注解限制:配置類因@ConditionalOnClass等條件未滿足而跳過。
  4. 包掃描路徑錯誤:模塊B未掃描到模塊A的包路徑。

二、解決方案

2.1 步驟1:聲明自動配置類

2.1.1 使用META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

在模塊A的src/main/resources目錄下創(chuàng)建以下路徑:

src/main/resources/
└── META-INF/
    └── spring/
        └── org.springframework.boot.autoconfigure.AutoConfiguration.imports

文件內(nèi)容為一行一個自動配置類的全限定名:

com.example.moduleA.config.ResourcesConfig

2.1.2 代碼示例:自動配置類

// 模塊A的ResourcesConfig.java
@AutoConfiguration
@ConditionalOnWebApplication(type = Type.SERVLET) // 僅在Servlet環(huán)境生效
public class ResourcesConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 全局性能攔截器
        registry.addInterceptor(new PlusWebInvokeTimeInterceptor())
                .addPathPatterns("/**") // 攔截所有路徑
                .excludePathPatterns("/actuator/**"); // 排除監(jiān)控端點
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 靜態(tài)資源處理(如Swagger)
        registry.addResourceHandler("/docs/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
    }

    /**
     * 跨域配置(通過@Bean注冊)
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOriginPattern("*"); // 允許所有源
        config.addAllowedHeader("*"); // 允許所有請求頭
        config.addAllowedMethod("*"); // 允許所有HTTP方法
        config.setMaxAge(1800L); // 預(yù)檢請求緩存時間
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}

2.2 步驟2:確保全局異常處理器生效

2.2.1 全局異常處理器代碼

// 模塊A的GlobalExceptionHandler.java
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseEntity<String> handleHttpRequestMethodNotSupported(
            HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
        log.error("請求地址'{}', 不支持'{}'請求", request.getRequestURI(), e.getMethod());
        return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED)
                .body("請求方法不支持: " + e.getMethod());
    }

    @ExceptionHandler(ServiceException.class)
    public ResponseEntity<ErrorResponse> handleServiceException(
            ServiceException e, HttpServletRequest request) {
        log.error("業(yè)務(wù)異常: {}", e.getMessage());
        return ResponseEntity.status(e.getStatusCode())
                .body(new ErrorResponse(e.getCode(), e.getMessage()));
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleGlobalException(Exception e) {
        log.error("全局異常: {}", e.getMessage(), e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body("系統(tǒng)內(nèi)部錯誤");
    }

    private static class ErrorResponse {
        private final int code;
        private final String message;

        public ErrorResponse(int code, String message) {
            this.code = code;
            this.message = message;
        }
    }
}

2.3 步驟3:檢查依賴傳遞與沖突

2.3.1 排除間接依賴沖突

假設(shè)模塊B引用了mybatis-spring-boot-starter,而該依賴間接引入了spring-boot-starter-logging(導(dǎo)致日志框架沖突)。需在POM中排除:

<!-- 模塊B的pom.xml -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.1</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2.3.2 檢查依賴樹

使用Maven或Gradle命令查看依賴樹:

# Maven
mvn dependency:tree | grep -i logback

# Gradle
./gradlew dependencies --configuration compileClasspath

2.4 步驟4:確保包掃描路徑正確

2.4.1 顯式指定掃描路徑

在模塊B的啟動類中設(shè)置scanBasePackages

// 模塊B的啟動類
@SpringBootApplication(
    scanBasePackages = {
        "com.example.moduleA.config",
        "com.example.moduleA.handler",
        "com.example.moduleB.controller"
    }
)
public class ModuleBApplication {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(ModuleBApplication.class);
        application.setAdditionalProfiles("dev"); // 激活開發(fā)環(huán)境配置
        application.run(args);
    }
}

2.4.2 包結(jié)構(gòu)優(yōu)化

確保模塊A的包路徑是模塊B啟動類的子包:

com.example
├── moduleA
│   ├── config
│   │   └── ResourcesConfig.java
│   └── handler
│       └── GlobalExceptionHandler.java
└── moduleB
    ├── controller
    │   └── UserController.java
    └── ModuleBApplication.java

2.5 步驟5:驗證條件注解

2.5.1 示例:基于屬性的條件

// 模塊A的FeatureAutoConfiguration.java
@Configuration
@ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
public class FeatureAutoConfiguration {
    @Bean
    public FeatureService featureService() {
        return new FeatureServiceImpl();
    }
}

application.yml中激活條件:

feature:
  enabled: true

2.6 步驟6:IDEA路徑問題排查

2.6.1 確保目錄結(jié)構(gòu)正確

  • 錯誤路徑:IDEA可能將META-INF/spring顯示為META-INF.spring
  • 解決方法
    1. 刪除錯誤路徑。
    2. 右鍵src/main/resources → New → Directory → 輸入META-INF/spring。
    3. 創(chuàng)建AutoConfiguration.imports文件。

三、核心知識點詳解

3.1 Spring Boot自動配置機(jī)制

3.1.1 核心組件

  1. 條件注解
    • @ConditionalOnClass:類路徑存在指定類時生效。
    • @ConditionalOnMissingBean:容器中無指定Bean時生效。
    • @ConditionalOnProperty:屬性存在且符合條件時生效。
    • @ConditionalOnWebApplication:僅在Web環(huán)境生效。
  2. 自動配置類
    • 通過@AutoConfiguration標(biāo)注,配合AutoConfiguration.imports文件聲明。
  3. 加載流程
    • Spring Boot 2.x:通過spring.factories文件加載自動配置類。
    • Spring Boot 3.x:推薦使用AutoConfiguration.imports

3.1.2 新舊機(jī)制對比

特性spring.factories(舊)AutoConfiguration.imports(新)
文件路徑META-INF/spring.factoriesMETA-INF/spring/org...AutoConfiguration.imports
格式需聲明EnableAutoConfiguration鍵直接列出類名,無需鍵值對
性能全局掃描,可能加載冗余配置按需加載,性能更優(yōu)

3.2 依賴管理

3.2.1 排除依賴沖突

<!-- 模塊B的pom.xml -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3.2.2 引入必要依賴

<!-- 引入log4j2 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

3.3 條件注解進(jìn)階用法

3.3.1 組合條件

@Configuration
@ConditionalOnClass(RedisClient.class) // 類路徑存在RedisClient
@ConditionalOnProperty(name = "redis.enabled", matchIfMissing = true) // 屬性存在或未配置時生效
public class RedisAutoConfig {
    // 配置邏輯
}

3.3.2 優(yōu)先級控制

@AutoConfiguration(after = AnotherConfig.class)
public class MyConfig {
    // 該配置類將在AnotherConfig之后加載
}

3.4 多模塊包掃描

3.4.1 動態(tài)掃描策略

// 使用@Import動態(tài)導(dǎo)入配置類
@Import({DatabaseAutoConfiguration.class, LoggingAutoConfiguration.class})
public class ModuleBApplication {
    // ...
}

四、常見陷阱與解決方案

4.1 陷阱1:IDEA路徑錯誤

  • 現(xiàn)象META-INF/spring目錄被錯誤顯示為META-INF.spring
  • 解決
    1. 刪除錯誤路徑。
    2. 右鍵src/main/resources → New → Directory → 輸入META-INF/spring。
    3. 重新創(chuàng)建AutoConfiguration.imports文件。

4.2 陷阱2:配置文件覆蓋

  • 現(xiàn)象:模塊B的application.yml覆蓋模塊A的配置。
  • 解決
    1. 將模塊A的配置文件放置在src/main/resources/config/目錄下。
    2. 在模塊B中指定激活配置文件:

4.3 陷阱3:未激活Profile

  • 現(xiàn)象:環(huán)境特定配置未生效。
  • 解決
# 啟動時指定Profile
java -jar app.jar --spring.profiles.active=dev

4.4 陷阱4:Spring Boot 3.x兼容性問題

  • 現(xiàn)象spring.factories配置失效。
  • 解決

    將配置遷移到新路徑:

# 模塊A的META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.example.moduleA.config.ResourcesConfig
com.example.moduleA.handler.GlobalExceptionHandler

五、驗證配置生效的方法

5.1 啟動日志檢查

  • 日志級別
logging.level.root=DEBUG
  • 關(guān)鍵日志
Positive matches:
  - ResourcesConfig (com.example.moduleA.config.ResourcesConfig)
  - GlobalExceptionHandler (com.example.moduleA.handler.GlobalExceptionHandler)

5.2 Bean注入驗證

// 模塊B的測試類
@RestController
public class HealthCheckController {
    @Autowired
    private CorsFilter corsFilter;

    @GetMapping("/health/cors")
    public String health() {
        return "CorsFilter: " + corsFilter.getClass().getName();
    }
}

5.3 跨域配置測試

5.3.1 測試步驟

  • 發(fā)送帶有Origin頭的請求:
curl -H "Origin: https://test.com" -X GET http://localhost:8080/api/test

預(yù)期響應(yīng)頭

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS

5.4 異常處理器驗證

5.4.1 測試業(yè)務(wù)異常

// 模塊B的Controller
@RestController
public class TestController {
    @GetMapping("/test")
    public ResponseEntity<String> test() {
        throw new ServiceException("自定義異常", HttpStatus.BAD_REQUEST);
    }
}

預(yù)期響應(yīng)

{
    "code": 400,
    "message": "自定義異常"
}

六、完整解決方案代碼示例

6.1 模塊A的POM配置

<!-- 模塊A的pom.xml -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

6.2 模塊B的POM配置

<!-- 模塊B的pom.xml -->
<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>moduleA</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

6.3 模塊B的啟動類

// 模塊B的啟動類
@SpringBootApplication(
    scanBasePackages = {
        "com.example.moduleA.config",
        "com.example.moduleA.handler",
        "com.example.moduleB.controller"
    }
)
public class ModuleBApplication {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(ModuleBApplication.class);
        application.setAdditionalProfiles("dev"); // 激活開發(fā)環(huán)境配置
        application.run(args);
    }
}

6.4 模塊A的自動配置文件

# 模塊A的META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.example.moduleA.config.ResourcesConfig
com.example.moduleA.handler.GlobalExceptionHandler

七、總結(jié)與最佳實踐(代碼視角)

7.1 核心總結(jié)

  1. 自動配置類必須通過AutoConfiguration.imports顯式聲明
    • 例如:在模塊A的META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports中列出所有自動配置類。
  2. Bean注冊需通過@Bean或?qū)崿F(xiàn)WebMvcConfigurer
    • CorsFilter通過@Bean注冊,WebMvcConfigurer方法需在@AutoConfiguration類中實現(xiàn)。
  3. 包掃描路徑必須覆蓋所有模塊
    • 模塊B的啟動類需顯式掃描模塊A的包路徑。

7.2 最佳實踐代碼模板

7.2.1 模塊A的自動配置類模板

@AutoConfiguration
@ConditionalOnClass(DispatcherServlet.class) // 依賴Servlet環(huán)境
public class ResourcesConfig implements WebMvcConfigurer {

    // 跨域、攔截器等配置

    @Bean
    public GlobalExceptionHandler globalExceptionHandler() {
        return new GlobalExceptionHandler(); // 手動注冊異常處理器
    }
}

7.2.2 全局異常處理器模板

@Slf4j
@RestControllerAdvice(basePackages = {"com.example.moduleA", "com.example.moduleB"})
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleGlobalException(Exception e) {
        log.error("全局異常: {}", e.getMessage(), e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body("系統(tǒng)內(nèi)部錯誤");
    }
}

八、附錄:工具與命令

8.1 依賴樹分析

mvn dependency:tree

8.2 日志級別調(diào)試

java -jar app.jar --logging.level.root=DEBUG

8.3 模塊間依賴驗證

# Gradle檢查依賴沖突
./gradlew dependencies --configuration compileClasspath

九、補(bǔ)充

1. @AutoConfiguration注解詳解

1.1 基本概念

  • 定義
    @AutoConfiguration 是Spring Boot 3.x引入的注解,用于標(biāo)記一個類為自動配置類,其作用是簡化自動配置類的聲明,無需在META-INF/spring.factories中手動注冊。
  • 作用
    1. 自動注冊到Spring容器:被@AutoConfiguration標(biāo)注的類會被Spring Boot自動識別為自動配置類。
    2. 按需加載:結(jié)合條件注解(如@ConditionalOnClass),僅在滿足條件時生效。
    3. 模塊化配置:適用于多模塊項目,將配置邏輯封裝到獨立模塊中。

1.2 @AutoConfiguration與@Configuration的區(qū)別

特性@AutoConfiguration@Configuration
作用自動配置類,無需手動注冊普通配置類,需通過其他方式注冊
自動注冊自動注冊到Spring容器(需配合AutoConfiguration.imports)需顯式注冊(如通過組件掃描或@Import)
適用場景自動配置場景(如多模塊共享配置)通用配置類(如自定義Bean)
是否需要額外配置需在AutoConfiguration.imports文件中聲明無需額外配置(但需被Spring容器掃描)
依賴關(guān)系通常與@Conditional注解結(jié)合可獨立使用,但需手動管理Bean依賴

1.3 @AutoConfiguration的使用場景

場景1:多模塊共享配置

// 模塊A的ResourcesConfig.java
@AutoConfiguration
@ConditionalOnWebApplication(type = Type.SERVLET)
public class ResourcesConfig implements WebMvcConfigurer {
    // 跨域配置、攔截器等
}
  • 關(guān)鍵點
    • 通過@AutoConfiguration標(biāo)記為自動配置類。
    • 在模塊A的META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中聲明該類。
    • 模塊B引用模塊A后,無需手動導(dǎo)入,Spring Boot會自動加載。

場景2:替代舊版spring.factories

# Spring Boot 2.x的spring.factories(需手動注冊)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.moduleA.config.ResourcesConfig,\
com.example.moduleA.handler.GlobalExceptionHandler
  • Spring Boot 3.x的改進(jìn)
    使用@AutoConfiguration + AutoConfiguration.imports文件,無需維護(hù)冗長的spring.factories。

2. @Bean注解詳解

2.1 基本概念

  • 定義
    @Bean 是Spring框架的核心注解,用于將方法返回的對象注冊為Spring容器管理的Bean。
  • 作用
    1. 顯式聲明Bean:開發(fā)者通過方法定義Bean的創(chuàng)建邏輯。
    2. 依賴注入:方法參數(shù)支持依賴注入(如@Autowired)。
    3. 靈活控制:可指定Bean作用域、初始化方法、條件等。
    4. 作用域控制:通過@Scope指定Bean的作用域(如singleton、prototype)。

2.2 @Bean的使用場景

場景1:定義基礎(chǔ)Bean

@Configuration
public class DataSourceConfig {
    @Bean
    @ConditionalOnMissingBean(DataSource.class) // 僅當(dāng)容器中沒有DataSource時生效
    public DataSource dataSource() {
        HikariDataSource ds = new HikariDataSource();
        ds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        return ds;
    }
}
  • 關(guān)鍵點
    • 通過@BeandataSource()方法返回的HikariDataSource注冊為Bean。
    • 結(jié)合@ConditionalOnMissingBean避免與用戶自定義Bean沖突。

場景2:注冊第三方庫Bean

@Configuration
public class JsonConfig {
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        return mapper;
    }
}
  • 關(guān)鍵點
    • 將第三方庫的ObjectMapper注冊為Spring Bean,方便全局使用。

場景3:作用域控制

@Bean
@Scope("prototype") // 每次請求創(chuàng)建新實例
public MyPrototypeBean prototypeBean() {
    return new MyPrototypeBean();
}

3. @Configuration與@Bean的協(xié)作關(guān)系

3.1 基礎(chǔ)配置類結(jié)構(gòu)

@Configuration // 標(biāo)記為配置類
public class MyAutoConfiguration {
    @Bean // 定義Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}
  • 關(guān)鍵點
    • @Configuration是配置類的“身份標(biāo)識”。
    • @Bean是配置類中定義Bean的“工具”。

4. 為什么必須使用@AutoConfiguration?能否用@Configuration替代?

4.1 必須使用@AutoConfiguration的情況

  • 場景:在多模塊項目中,模塊A的配置需被模塊B自動加載
  • 原因
    1. @AutoConfiguration配合AutoConfiguration.imports文件,無需手動注冊。
    2. 如果僅用@Configuration,需通過以下方式顯式加載:
      • 在模塊B的啟動類中指定掃描路徑。
      • 通過@Import(ResourcesConfig.class)導(dǎo)入。
      • spring.factories中注冊(Spring Boot 2.x方式)。
  • 結(jié)論:在Spring Boot 3.x中,@AutoConfiguration是更簡潔的解決方案。

4.2 @Configuration的替代方案

// 方案1:在模塊B啟動類中顯式掃描模塊A的包
@SpringBootApplication(scanBasePackages = {"com.example.moduleA", "com.example.moduleB"})
public class ModuleBApplication { ... }

// 方案2:使用@Import導(dǎo)入配置類
@Configuration
@Import(ResourcesConfig.class)
public class ModuleBConfig { ... }
  • 缺點
    • 需手動維護(hù)包路徑或?qū)腙P(guān)系,耦合性更高。
    • 不具備條件化加載能力(除非手動添加@Conditional注解)。

5. 示例代碼中的關(guān)鍵注解解析

5.1 ResourcesConfig示例

@AutoConfiguration
@ConditionalOnWebApplication(type = Type.SERVLET)
public class ResourcesConfig implements WebMvcConfigurer {
    @Bean
    public CorsFilter corsFilter() { ... }

    @Override
    public void addInterceptors(InterceptorRegistry registry) { ... }
}
  • 關(guān)鍵點
    1. @AutoConfiguration:聲明為自動配置類。
    2. @ConditionalOnWebApplication:僅在Servlet環(huán)境生效。
    3. @Bean:將CorsFilter注冊為Bean。
    4. 實現(xiàn)WebMvcConfigurer:通過繼承接口直接擴(kuò)展Spring MVC配置。

5.2 全局異常處理器示例

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleGlobalException(Exception e) { ... }
}
  • 關(guān)鍵點
    • @RestControllerAdvice:標(biāo)記為全局異常處理類。
    • 無需@Bean,因為Spring通過組件掃描自動識別。

6. 常見問題與解答

Q1:為什么我的自動配置類未被加載?

  • 可能原因
    1. 未在AutoConfiguration.imports文件中聲明類。
    2. 依賴未正確引入(如模塊B未依賴模塊A)。
    3. 條件注解未滿足(如@ConditionalOnClass的類不存在)。
  • 解決方案
    • 檢查AutoConfiguration.imports文件路徑是否正確。
    • 確保模塊B的POM中包含模塊A的依賴。
    • 在啟動日志中搜索Positive matches,確認(rèn)條件是否滿足。

Q2:@Bean和@Component的區(qū)別?

注解作用范圍使用場景
@Bean方法級在配置類中定義Bean的創(chuàng)建邏輯
@Component類級通過組件掃描自動注冊Bean

Q3:為什么自動配置類需要實現(xiàn)WebMvcConfigurer?

  • 原因
    Spring MVC的擴(kuò)展機(jī)制允許通過實現(xiàn)WebMvcConfigurer接口或使用@Bean注冊WebMvcConfigurer來添加攔截器、跨域配置等。
  • 示例
@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new MyInterceptor());
}

7. 完整自動配置類代碼示例

// 模塊A的ResourcesConfig.java
@AutoConfiguration
@ConditionalOnWebApplication(type = Type.SERVLET)
public class ResourcesConfig implements WebMvcConfigurer {
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOriginPattern("*");
        config.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

    @Bean
    public MyInterceptor myInterceptor() {
        return new MyInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor())
                .addPathPatterns("/**");
    }

    @Bean
    @ConditionalOnMissingBean
    public MyService myService() {
        return new MyServiceImpl();
    }
}

8. 關(guān)鍵配置文件說明

8.1 AutoConfiguration.imports文件

# 模塊A的META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.example.moduleA.config.ResourcesConfig
  • 作用
    告知Spring Boot哪些類是自動配置類,無需手動注冊到spring.factories。

8.2 spring.factories(Spring Boot 2.x兼容)

# 模塊A的META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.moduleA.config.ResourcesConfig,\
com.example.moduleA.handler.GlobalExceptionHandler
  • 兼容性說明
    Spring Boot 3.x支持同時使用兩種方式,但推薦使用@AutoConfiguration。

以上就是SpringBoot多模塊自動配置失效問題的解決方案的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot多模塊自動配置失效的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論