java - 随机无法识别 Maven 依赖项

标签 java maven spring-boot intellij-idea dependencies

我正在开发一个使用 HTML 和 Thymeleaf 作为前端的 Java Spring Boot Web 应用程序。我遇到的问题是,当我尝试运行我的项目时,我的 ThymeleafConfig 类会随机出现错误。我将在下面进行更详细的解释,但首先这是我的代码。

Pom.xml 依赖:

<dependency>
     <groupId>org.thymeleaf.extras</groupId>
     <artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>

ThymeleafConfig 类:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect;

@Configuration
public class ThymeleafConfig {

    @Bean
    public SpringSecurityDialect springSecurityDialect() {
        return new SpringSecurityDialect();
    }
}

所以进一步解释一下,我的代码会很好(根据我的 IDE),我的任何行都没有错误,可以运行了。我将运行我的应用程序类来运行我的项目,但我会遇到编译错误。

IntelliJ 会自动打开其中有错误的文件,这是我的 ThymeleafConfig 类。出现错误时,导入语句 import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect; 行将显示为灰色且无法识别。导致类错误。

要修复它,我右键单击我的 pom.xml 文件 -> Maven -> Reimport。这将重新导入我的所有依赖项,一切都会恢复正常。

我还想强调的是,我每次运行应用程序时都不会发生这种情况。有时它会连续发生 3 次,有时我会运行 5 次以上,然后错误再次出现。

我已经尝试过 mvn clean,但没有解决问题。我还将依赖项移动到 pom.xml 文件中的不同行。

编辑: 这是我的 Application.java 类

@SpringBootApplication
public class Application {

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

要运行该应用程序,我右键单击该文件并单击运行“应用程序”按钮。

最佳答案

Spring boot 已经包含 thymeleaf(一个不同的版本),您可能在类路径上有多个版本。 Spring Boot 在其文档中非常清楚地介绍了 thymeleaf:

Spring boot 1.4.2's documentation关于使用 thymeleaf 3.0.0

By default, spring-boot-starter-thymeleaf uses Thymeleaf 2.1. If you are using the spring-boot-starter-parent, you can use Thymeleaf 3 by overriding the thymeleaf.version and thymeleaf-layout-dialect.version properties, for example:

<properties>
    <thymeleaf.version>3.0.0.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.0.3</thymeleaf-layout-dialect.version>
</properties>

...

If you are using any of the other auto-configured Thymeleaf Extras (Spring Security, Data Attribute, or Java 8 Time) you should also override each of their versions to one that is compatible with Thymeleaf 3.0.

如果你看spring-boot-dependency's pom on github ,百里香叶的特性是:

<thymeleaf.version>2.1.5.RELEASE</thymeleaf.version>          
<thymeleaf-extras-springsecurity4.version>2.1.2.RELEASE</thymeleaf-extras-springsecurity4.version>

<thymeleaf-extras-conditionalcomments.version>2.1.2.RELEASE</thymeleaf-extras-conditionalcomments.version>

<thymeleaf-layout-dialect.version>1.4.0</thymeleaf-layout-dialect.version>

<thymeleaf-extras-data-attribute.version>1.3</thymeleaf-extras-data-attribute.version>

<thymeleaf-extras-java8time.version>2.1.0.RELEASE</thymeleaf-extras-java8time.version>

回顾

我相信您至少需要定义以下属性(也许还要定义几个属性以避免版本不匹配?):

  • thymeleaf.version
  • thymeleaf-extras-springsecurity4.version

关于java - 随机无法识别 Maven 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40024478/

相关文章:

android - Eclipse 中的 Maven 插件错误

java - SpringBoot - 完全停用安全性以启用摘要转发

java - IntelliJ 社区找不到生成 WAR 的 Web 应用程序工件

mongodb - 为什么在 Spring 响应式 Mongo 中订阅有效而阻止无效?

java - 如何最好地使用 Java 部分验证电话号码

java - 如何使用java执行shell脚本

java.lang.ClassNotFoundException : org. apache.hive.service.cli.HiveSQLException 异常

maven - 两个版本 :prepare and release:perform 上的配置文件激活

java - 在不扩展它的类中使用抽象类方法

java - 如何在android上为多个复选框动态添加 Action ?