javaspringspring-bootcasspring-autoconfiguration

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. When no CAS dependency is present the application configures as expected. 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).

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

@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");
    }
}

DemoConfiguration.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 is empty.

When removing the org.apereo.cas dependency the app starts just fine and the beans are autowired as expected.

Is there anything that disables Spring Boot autoconfiguration / component scan? I don't want the dependencies to mess up my own configuration and would like to prevent this behaviour.


Solution

  • CAS version 6.4.5 uses Spring 5 like the project itself with Spring Boot version 2.5.9. Spring 5 has added a component index at compile time feature which is used by 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

    <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. For further details see https://stackoverflow.com/a/48407939

    and

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