简体   繁体   English

Spring 启动应用程序中的依赖项阻止组件扫描

[英]Dependency prevents component scan in Spring Boot Application

I build a minimal example which shows the problem I faced the last days.我建立了一个最小的例子来展示我最近几天遇到的问题。 In short, all dependencies of apereo CAS that I tried prevent my Spring Boot Application from auto configuration or from creating the components and beans.简而言之,我尝试过的 apereo CAS 的所有依赖项都会阻止我的 Spring 引导应用程序自动配置或创建组件和 bean。 When no CAS dependency is present the application configures as expected.当不存在 CAS 依赖项时,应用程序会按预期进行配置。 It doesn't matter whether I use any classes from that dependency or not, just to have the dependency present messes things up.我是否使用该依赖项中的任何类都没有关系,只是让依赖项存在会把事情搞砸。

I created a demo project with the spring inizializr with Spring Boot version 2.5.9, Java 11, Maven and Spring Web dependency (see below).我使用 spring inizializr 和 Spring Boot version 2.5.9、Java 11、Maven 和 Spring Web 依赖项(见下文)创建了一个演示项目。

Structure结构

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

DemoApplication.java演示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 BeanComponent.java

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

BeanWithoutComponent.java BeanWithoutComponent.java

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

DemoConfiguration.java演示配置.java

@Configuration
public class DemoConfiguration {

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

pom.xml 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 is empty. application.properties 为空。

When removing the org.apereo.cas dependency the app starts just fine and the beans are autowired as expected.删除 org.apereo.cas 依赖项时,应用程序可以正常启动,并且 bean 会按预期自动装配。

Is there anything that disables Spring Boot autoconfiguration / component scan?有什么东西可以禁用 Spring 引导自动配置/组件扫描吗? I don't want the dependencies to mess up my own configuration and would like to prevent this behaviour.我不希望依赖项弄乱我自己的配置,并希望防止这种行为。

CAS version 6.4.5 uses Spring 5 like the project itself with Spring Boot version 2.5.9. CAS 版本 6.4.5 使用 Spring 5,就像项目本身使用 Spring 引导版本 2.5.9。 Spring 5 has added a component index at compile time feature which is used by CAS. Spring 5 添加了 CAS 使用的编译时组件索引功能。

Since the project does not include the auto indexer dependency the components aren't found at compile time and therefore not present at startup.由于该项目不包括自动索引器依赖项,因此在编译时找不到组件,因此在启动时不存在。

The solution to this problem is including this dependency in the pom.xml此问题的解决方案是在 pom.xml 中包含此依赖项

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

This was discussed and answered by other stackoverflow posts.其他 stackoverflow 帖子对此进行了讨论和回答。 For further details see https://stackoverflow.com/a/48407939有关详细信息,请参阅https://stackoverflow.com/a/48407939

and

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

Just looked at the sources for this dependency and there are a few configuration files.只需查看此依赖项的来源,就会有一些配置文件。

You could attempt to exclude the configuration files which you do not want:您可以尝试排除不需要的配置文件:

Example:例子:

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM