簡體   English   中英

Spring 啟動應用程序中的依賴項阻止組件掃描

[英]Dependency prevents component scan in Spring Boot Application

我建立了一個最小的例子來展示我最近幾天遇到的問題。 簡而言之,我嘗試過的 apereo CAS 的所有依賴項都會阻止我的 Spring 引導應用程序自動配置或創建組件和 bean。 當不存在 CAS 依賴項時,應用程序會按預期進行配置。 我是否使用該依賴項中的任何類都沒有關系,只是讓依賴項存在會把事情搞砸。

我使用 spring inizializr 和 Spring Boot version 2.5.9、Java 11、Maven 和 Spring Web 依賴項(見下文)創建了一個演示項目。

結構

demo/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com.example.demo/
│   │   │       ├── beans/
│   │   │       │   ├── BeanComponent.java
│   │   │       │   └── BeanWithoutComponent.java
│   │   │       ├── config/
│   │   │       │   └── DemoConfiguration.java
│   │   │       └── DemoApplication.java
│   │   └── resources/
│   │       ├── static/
│   │       ├── templates/
│   │       └── application.properties
│   └── test/
│       └── java/
│           └── com.example.demo/
│               └── DemoApplicationTests.java
└── pom.xml

演示Application.java

@SpringBootApplication
public class DemoApplication {

    @Autowired
    private BeanWithoutComponent beanWithoutComponent;

    @Autowired
    private BeanComponent beanComponent;

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

BeanComponent.java

@Component
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
public class BeanComponent {
    public BeanComponent() {
        System.out.println("BeanComponent created");
    }
}

BeanWithoutComponent.java

public class BeanWithoutComponent {
    public BeanWithoutComponent() {
        System.out.println("BeanWithoutComponent created");
    }
}

演示配置.java

@Configuration
public class DemoConfiguration {

    @Bean
    public BeanWithoutComponent beanWithoutComponent() {
        return new BeanWithoutComponent();
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.9</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <cas.version>6.4.5</cas.version>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-core-services</artifactId>
            <version>${cas.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties 為空。

刪除 org.apereo.cas 依賴項時,應用程序可以正常啟動,並且 bean 會按預期自動裝配。

有什么東西可以禁用 Spring 引導自動配置/組件掃描嗎? 我不希望依賴項弄亂我自己的配置,並希望防止這種行為。

CAS 版本 6.4.5 使用 Spring 5,就像項目本身使用 Spring 引導版本 2.5.9。 Spring 5 添加了 CAS 使用的編譯時組件索引功能。

由於該項目不包括自動索引器依賴項,因此在編譯時找不到組件,因此在啟動時不存在。

此問題的解決方案是在 pom.xml 中包含此依賴項

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-indexer</artifactId>
        <version>5.3.16</version>
        <optional>true</optional>
    </dependency>
</dependencies>

其他 stackoverflow 帖子對此進行了討論和回答。 有關詳細信息,請參閱https://stackoverflow.com/a/48407939

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-scanning-index

只需查看此依賴項的來源,就會有一些配置文件。

您可以嘗試排除不需要的配置文件:

例子:

@SpringBootApplication(exclude = {
    CasCoreServicesConfiguration.class, 
})

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM