SpringBoot启动异常:IllegalStateException could not evaluate condition on DevToolsDataSourceAutoConfiguration

我曾试图用IDEA启动我的Spring Boot应用程序。然而,无论我尝试用什么办法来解决这个问题,我似乎都无法使其正常工作。我相信这与spring.autoconfigure包有关,但根本问题对我来说仍然是个谜。

我试图利用Maven多模块系统Java平台模块系统。以下是我的pom.xml、module-info.java文件和项目结构。

-

请注意,有一个父pom.xml文件,每个子模块都有自己的pom.xml文件。每个子模块也有自己的module-info.java文件。

Parent 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>
    <packaging>pom</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1</version>
    <name>Demo Project</name>
    <description>Demo Project Description</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <!--Add each module here -->
    <modules>
        <module>webapp</module>
        <module>persistence</module>
        <module>facade</module>
        <module>service</module>
        <module>security</module>
        <module>domain</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.5.1</version>
                    <configuration>
                        <mainClass>com.example.MainApplication</mainClass>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
                <plugin>
                    <!-- For testing -->
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M5</version>
                    <configuration>
                        <testFailureIgnore>true</testFailureIgnore>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

webapp 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <!--    <version>0.0.1</version>-->

    <artifactId>webapp</artifactId>

    <dependencies>
        <!-- Add dependencies for `webapp` module here -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>facade</artifactId>
            <version>0.0.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>domain</artifactId>
            <version>0.0.1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

webapp module-info.java

module webapp {
    requires domain;
    requires facade;

    requires spring.web;
    requires spring.boot;
    requires spring.boot.autoconfigure;
    requires spring.context;
}

以下是我所尝试的:因此,当我按原样运行应用程序时,我得到了以下异常

Exception in thread "restartedMain" java.lang.IllegalAccessException: class org.springframework.boot.devtools.restart.RestartLauncher (in module spring.boot.devtools) cannot access class com.example.MainApplication (in module webapp) because module webapp does not export com.example to module spring.boot.devtools
    at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
    at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591)
    at java.base/java.lang.reflect.Method.invoke(Method.java:558)
    at spring.boot.devtools@2.5.1/org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

我为解决这个问题所做的是把webapp/module-info.java

odule webapp {
}

修改为

open module webapp {
}

但后来,我得到了以下异常情况

java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.devtools.autoconfigure.DevToolsDataSourceAutoConfiguration due to javax/sql/DataSource not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
    at spring.boot.autoconfigure@2.5.1/org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:54) ~[spring-boot-autoconfigure-2.5.1.jar:na]
    at spring.context@5.3.8/org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108) ~[spring-context-5.3.8.jar:na]
    at spring.context@5.3.8/org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader$TrackedConditionEvaluator.shouldSkip(ConfigurationClassBeanDefinitionReader.java:489) ~[spring-context-5.3.8.jar:na]
    at spring.context@5.3.8/org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:140) ~[spring-context-5.3.8.jar:na]
    at spring.context@5.3.8/org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:129) ~[spring-context-5.3.8.jar:na]
    at spring.context@5.3.8/org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:343) ~[spring-context-5.3.8.jar:na]
    at spring.context@5.3.8/org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247) ~[spring-context-5.3.8.jar:na]
    at spring.context@5.3.8/org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311) ~[spring-context-5.3.8.jar:na]
    at spring.context@5.3.8/org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112) ~[spring-context-5.3.8.jar:na]
    at spring.context@5.3.8/org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746) ~[spring-context-5.3.8.jar:na]
    at spring.context@5.3.8/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564) ~[spring-context-5.3.8.jar:na]
    at spring.boot@2.5.1/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.1.jar:na]
    at spring.boot@2.5.1/org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.1.jar:na]
    at spring.boot@2.5.1/org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.1.jar:na]
    at spring.boot@2.5.1/org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.1.jar:na]
    at spring.boot@2.5.1/org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.1.jar:na]
    at webapp/com.example.MainApplication.main(MainApplication.java:10) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at spring.boot.devtools@2.5.1/org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.5.1.jar:na]
Caused by: java.lang.NoClassDefFoundError: javax/sql/DataSource
    at spring.boot.devtools@2.5.1/org.springframework.boot.devtools.autoconfigure.DevToolsDataSourceAutoConfiguration$DevToolsDataSourceCondition.getMatchOutcome(DevToolsDataSourceAutoConfiguration.java:183) ~[spring-boot-devtools-2.5.1.jar:na]
    at spring.boot.autoconfigure@2.5.1/org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47) ~[spring-boot-autoconfigure-2.5.1.jar:na]
    ... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.sql.DataSource
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[na:na]
    ... 23 common frames omitted
  • 我试着设置spring.devtools.restart.enabled=false。同样的错误接踵而来。
  • 我试着把spring-boot-devtools从依赖列表中移除。没有错误日志,但启动过程退出时错误代码为: 0
  • 我试着添加h2数据库的依赖性,只是想看看会发生什么。没有效果。
  • 我试着改变spring-boot-devtools<scope>值,甚至把它注释出来。没有效果。
  • 我试着从AutoConfiguration中排除DevToolsDataSourceAutoConfiguration.class。没有效果。
  • 我试着在module-info.java中加入exports com.example;。没有效果。
  • 如果我运行mvn testmvn clean installmvn spring-boot:run,我没有得到一个错误,服务器运行正常。
  • MainApplication.java位于webapp/src/main/java/com/example/MainApplication.java
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


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

为什么spring.autoconfigure试图找到一个DataSource?是不是IntelliJ的classpath混乱了,我需要手动更新它的依赖关系?如果我配置了一个合适的数据库(比如mssql),我还会继续面临这个问题吗?

最让我困惑的是,我输入mvn spring-boot:run命令,触发服务器运行时没有错误。说实话,这让我觉得问题与IntelliJclasspath设置有关,而不是项目结构和配置。

如果能得到任何帮助,我将不胜感激。谢谢。