最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot多模塊自動(dòng)配置失效問(wèn)題的解決方案

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

一、問(wèn)題背景與場(chǎng)景

1.1 場(chǎng)景描述

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

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

1.2 核心問(wèn)題

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

二、解決方案

2.1 步驟1:聲明自動(dòng)配置類(lèi)

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)容為一行一個(gè)自動(dòng)配置類(lèi)的全限定名:

com.example.moduleA.config.ResourcesConfig

2.1.2 代碼示例:自動(dòng)配置類(lèi)

// 模塊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)控端點(diǎn)
    }

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

    /**
     * 跨域配置(通過(guò)@Bean注冊(cè))
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOriginPattern("*"); // 允許所有源
        config.addAllowedHeader("*"); // 允許所有請(qǐng)求頭
        config.addAllowedMethod("*"); // 允許所有HTTP方法
        config.setMaxAge(1800L); // 預(yù)檢請(qǐng)求緩存時(shí)間
        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("請(qǐng)求地址'{}', 不支持'{}'請(qǐng)求", request.getRequestURI(), e.getMethod());
        return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED)
                .body("請(qǐng)求方法不支持: " + 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)部錯(cuò)誤");
    }

    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:檢查依賴(lài)傳遞與沖突

2.3.1 排除間接依賴(lài)沖突

假設(shè)模塊B引用了mybatis-spring-boot-starter,而該依賴(lài)間接引入了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 檢查依賴(lài)樹(shù)

使用Maven或Gradle命令查看依賴(lài)樹(shù):

# Maven
mvn dependency:tree | grep -i logback

# Gradle
./gradlew dependencies --configuration compileClasspath

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

2.4.1 顯式指定掃描路徑

在模塊B的啟動(dòng)類(lèi)中設(shè)置scanBasePackages

// 模塊B的啟動(dòng)類(lèi)
@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"); // 激活開(kāi)發(fā)環(huán)境配置
        application.run(args);
    }
}

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

確保模塊A的包路徑是模塊B啟動(dòng)類(lèi)的子包:

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

2.5 步驟5:驗(yàn)證條件注解

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路徑問(wèn)題排查

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

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

三、核心知識(shí)點(diǎn)詳解

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

3.1.1 核心組件

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

3.1.2 新舊機(jī)制對(duì)比

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

3.2 依賴(lài)管理

3.2.1 排除依賴(lài)沖突

<!-- 模塊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 引入必要依賴(lài)

<!-- 引入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) // 類(lèi)路徑存在RedisClient
@ConditionalOnProperty(name = "redis.enabled", matchIfMissing = true) // 屬性存在或未配置時(shí)生效
public class RedisAutoConfig {
    // 配置邏輯
}

3.3.2 優(yōu)先級(jí)控制

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

3.4 多模塊包掃描

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

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

四、常見(jiàn)陷阱與解決方案

4.1 陷阱1:IDEA路徑錯(cuò)誤

  • 現(xiàn)象META-INF/spring目錄被錯(cuò)誤顯示為META-INF.spring
  • 解決
    1. 刪除錯(cuò)誤路徑。
    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)境特定配置未生效。
  • 解決
# 啟動(dòng)時(shí)指定Profile
java -jar app.jar --spring.profiles.active=dev

4.4 陷阱4:Spring Boot 3.x兼容性問(wèn)題

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

    將配置遷移到新路徑:

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

五、驗(yàn)證配置生效的方法

5.1 啟動(dòng)日志檢查

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

5.2 Bean注入驗(yàn)證

// 模塊B的測(cè)試類(lèi)
@RestController
public class HealthCheckController {
    @Autowired
    private CorsFilter corsFilter;

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

5.3 跨域配置測(cè)試

5.3.1 測(cè)試步驟

  • 發(fā)送帶有Origin頭的請(qǐng)求:
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 異常處理器驗(yàn)證

5.4.1 測(cè)試業(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的啟動(dòng)類(lèi)

// 模塊B的啟動(dòng)類(lèi)
@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"); // 激活開(kāi)發(fā)環(huán)境配置
        application.run(args);
    }
}

6.4 模塊A的自動(dòng)配置文件

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

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

7.1 核心總結(jié)

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

7.2 最佳實(shí)踐代碼模板

7.2.1 模塊A的自動(dòng)配置類(lèi)模板

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

    // 跨域、攔截器等配置

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

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)部錯(cuò)誤");
    }
}

八、附錄:工具與命令

8.1 依賴(lài)樹(shù)分析

mvn dependency:tree

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

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

8.3 模塊間依賴(lài)驗(yàn)證

# Gradle檢查依賴(lài)沖突
./gradlew dependencies --configuration compileClasspath

九、補(bǔ)充

1. @AutoConfiguration注解詳解

1.1 基本概念

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

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

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

1.3 @AutoConfiguration的使用場(chǎng)景

場(chǎng)景1:多模塊共享配置

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

場(chǎng)景2:替代舊版spring.factories

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

2. @Bean注解詳解

2.1 基本概念

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

2.2 @Bean的使用場(chǎng)景

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

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

場(chǎng)景2:注冊(cè)第三方庫(kù)Bean

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

場(chǎng)景3:作用域控制

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

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

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

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

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

4.1 必須使用@AutoConfiguration的情況

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

4.2 @Configuration的替代方案

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

// 方案2:使用@Import導(dǎo)入配置類(lèi)
@Configuration
@Import(ResourcesConfig.class)
public class ModuleBConfig { ... }
  • 缺點(diǎn)
    • 需手動(dòng)維護(hù)包路徑或?qū)腙P(guān)系,耦合性更高。
    • 不具備條件化加載能力(除非手動(dòng)添加@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)鍵點(diǎn)
    1. @AutoConfiguration:聲明為自動(dòng)配置類(lèi)。
    2. @ConditionalOnWebApplication:僅在Servlet環(huán)境生效。
    3. @Bean:將CorsFilter注冊(cè)為Bean。
    4. 實(shí)現(xiàn)WebMvcConfigurer:通過(guò)繼承接口直接擴(kuò)展Spring MVC配置。

5.2 全局異常處理器示例

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleGlobalException(Exception e) { ... }
}
  • 關(guān)鍵點(diǎn)
    • @RestControllerAdvice:標(biāo)記為全局異常處理類(lèi)。
    • 無(wú)需@Bean,因?yàn)镾pring通過(guò)組件掃描自動(dòng)識(shí)別。

6. 常見(jiàn)問(wèn)題與解答

Q1:為什么我的自動(dòng)配置類(lèi)未被加載?

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

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

注解作用范圍使用場(chǎng)景
@Bean方法級(jí)在配置類(lèi)中定義Bean的創(chuàng)建邏輯
@Component類(lèi)級(jí)通過(guò)組件掃描自動(dòng)注冊(cè)Bean

Q3:為什么自動(dòng)配置類(lèi)需要實(shí)現(xiàn)WebMvcConfigurer?

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

7. 完整自動(dòng)配置類(lèi)代碼示例

// 模塊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)鍵配置文件說(shuō)明

8.1 AutoConfiguration.imports文件

# 模塊A的META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.example.moduleA.config.ResourcesConfig
  • 作用
    告知Spring Boot哪些類(lèi)是自動(dòng)配置類(lèi),無(wú)需手動(dòng)注冊(cè)到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
  • 兼容性說(shuō)明
    Spring Boot 3.x支持同時(shí)使用兩種方式,但推薦使用@AutoConfiguration。

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

相關(guān)文章

  • Java使用BigDecimal公式精確計(jì)算及精度丟失問(wèn)題

    Java使用BigDecimal公式精確計(jì)算及精度丟失問(wèn)題

    在工作中經(jīng)常會(huì)遇到數(shù)值精度問(wèn)題,比如說(shuō)使用float或者double的時(shí)候,可能會(huì)有精度丟失問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于Java使用BigDecimal公式精確計(jì)算及精度丟失問(wèn)題的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • Java IO三大模型(BIO/NIO/AIO)的使用總結(jié)

    Java IO三大模型(BIO/NIO/AIO)的使用總結(jié)

    相信很多Java開(kāi)發(fā)剛接觸IO模型時(shí),都會(huì)被「BIO、NIO、AIO」「同步、異步、阻塞、非阻塞」這些概念繞暈,下面就來(lái)詳細(xì)的介紹一下 IO三大模型的使用,感興趣的可以了解一下
    2026-02-02
  • Java實(shí)現(xiàn)按鍵精靈的示例代碼

    Java實(shí)現(xiàn)按鍵精靈的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)按鍵精靈,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定的參考價(jià)值,感興趣的可以學(xué)習(xí)一下
    2022-05-05
  • Spring配置文件無(wú)法讀取properties屬性的解決

    Spring配置文件無(wú)法讀取properties屬性的解決

    這篇文章主要介紹了Spring配置文件無(wú)法讀取properties屬性的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Spring Security 在 Spring Boot 中的使用詳解【集中式】

    Spring Security 在 Spring Boot 中的使用詳解【集中式】

    這篇文章主要介紹了Spring Security 在 Spring Boot 中的使用【集中式】,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • ehcache模糊批量移除緩存的方法

    ehcache模糊批量移除緩存的方法

    本篇文章主要介紹了ehcache模糊批量移除緩存的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • Spring用AspectJ開(kāi)發(fā)AOP(基于Annotation)

    Spring用AspectJ開(kāi)發(fā)AOP(基于Annotation)

    這篇文章主要介紹了Spring用AspectJ開(kāi)發(fā)AOP(基于Annotation),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • 如何將Java對(duì)象轉(zhuǎn)換成JSON

    如何將Java對(duì)象轉(zhuǎn)換成JSON

    這篇文章主要介紹了如何將Java對(duì)象轉(zhuǎn)換成JSON,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-11-11
  • springboot+vue+elementsUI實(shí)現(xiàn)分角色注冊(cè)登錄界面功能

    springboot+vue+elementsUI實(shí)現(xiàn)分角色注冊(cè)登錄界面功能

    這篇文章主要給大家介紹了關(guān)于springboot+vue+elementsUI實(shí)現(xiàn)分角色注冊(cè)登錄界面功能的相關(guān)資料,Spring?Boot和Vue.js是兩個(gè)非常流行的開(kāi)源框架,可以用來(lái)構(gòu)建Web應(yīng)用程序,需要的朋友可以參考下
    2023-07-07
  • Java類(lèi)之間的關(guān)系圖_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java類(lèi)之間的關(guān)系圖_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    在Java以及其他的面向?qū)ο笤O(shè)計(jì)模式中,類(lèi)與類(lèi)之間主要有6種關(guān)系,他們分別是:依賴(lài)、關(guān)聯(lián)、聚合、組合、繼承、實(shí)現(xiàn)。他們的耦合度依次增強(qiáng),有興趣的可以了解一下
    2017-08-08

最新評(píng)論

衡水市| 平湖市| 团风县| 娄底市| 古田县| 托里县| 江门市| 沐川县| 涿鹿县| 张家港市| 浠水县| 临潭县| 克山县| 沂源县| 安丘市| 东兰县| 广西| 建阳市| 周至县| 长海县| 阳新县| 招远市| 诸城市| 南城县| 离岛区| 翼城县| 利津县| 延边| 天气| 新安县| 仪陇县| 苗栗县| 武隆县| 红桥区| 台东县| 宁安市| 沙坪坝区| 湘阴县| 和硕县| 疏附县| 本溪|