【问题标题】:thymeleaf error百里香叶错误
【发布时间】:2016-08-14 08:00:44
【问题描述】:

以下代码抛出无法解析的循环引用的错误。当 Spring 尝试创建 MessageFormatHelper 类的 bean 时。运行 jar 时会抛出此错误。我试图看到但没有得到什么是原因。谁能帮忙。

头等舱

@Component
class DbTemplateResolver extends TemplateResolver {
@Autowired 
SpringTemplateEngine templateEngine;
....othercode
@PostConstruct
    public void extension() {
        templateEngine.addTemplateResolver(this);
    }
...other code
}

二等,

@Component
class MessageFormatHelper{

@Autowired
  SpringTemplateEngine templateEngine;
... other code
String getMessage()
{
        final Context ctx = new Context(locale);
                ctx.setVariable("contractMap", model.get(ContractMap.TEMPLATE_MODEL_MAP_KEY));
                mergedMessage = templateEngine.process(fileName, ctx);
}

}

完全错误:

原因:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.flex.eventManagement.handler.helper.MessageFormatHelper com.flex.eventManagement.handler.helper.NotificationPreProcessor.messageFormatHelper;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“messageFormatHelper”的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:org.thymeleaf.spring4.SpringTemplateEngine com.flex.eventManagement.handler.helper.MessageFormatHelper.templateEngine;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$ThymeleafDefaultConfiguration' 的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有最终 java.util.Collection org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$ThymeleafDefaultConfiguration.templateResolvers;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“dbTemplateResolver”的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:org.thymeleaf.spring4.SpringTemplateEngine com.flex.eventManagement.handler.helper.DbTemplateResolver.templateEngine;嵌套异常是 org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为“templateEngine”的 bean 时出错:当前正在创建请求的 bean:是否存在无法解析的循环引用?

更新 从 DbTemplateResolver 类中删除模板引擎自动连接。如下所示自动装配到 MessageFormatHelper.java 类中

class MessageFormatHelper{

 @Bean
  public DbTemplateResolver dbTemplateResolver() {
    DbTemplateResolver resolver = new DbTemplateResolver();
      resolver.setOrder(2);
      return resolver;
  }

  @Bean
  public SpringTemplateEngine thymeleafTemplateEngine() {
      SpringTemplateEngine engine = new SpringTemplateEngine();
      engine.setTemplateResolvers(Sets.newHashSet(dbTemplateResolver()));
      return engine;
  }
}

我还需要从 MessageFormatHelper 中删除 SpringTemplateEngine 自动接线,对吗?那我怎么打电话给mergedMessage = templateEngine.process(fileName, ctx);?

更新 2。 应遵循 MessageFormatHelper

中的要求
  @Autowired
  DbTemplateResolver dbTemplateResolver;

  @Autowired
  SpringTemplateEngine templateEngine;
@PostConstruct
    public void extension() {
        templateEngine.addTemplateResolver(dbTemplateResolver);
    }

【问题讨论】:

  • 当然它会而且它永远不会像这样工作......您正在创建一个TemplateResolver,这是构建SpringTemplateEngine所必需的,但您希望它是自动连接的......所以不,它永远不会起作用。只需将 DbResolver 设为 bean 并在配置 lass 中对其进行配置,然后将其连接到 SpringTemplateEngine 而不是这样。恕我直言,这首先是糟糕的编程。如果您使用 Spring Boot,您只需将 DbResolver 声明为 bean,它将自动为您注册。基本上你正在让它变得复杂。
  • 您不需要这些 bean...Spring Boot 已经为您提供了它们...了解您正在使用的框架并使用该框架。删除 @Bean 方法并简单地删除 @Autowire MessageFormatHelper 中的模板解析器......我从来没有说过你应该改变它!
  • No... 如前所述删除@Bean 方法,spring boot 在检测到百里香叶时已经为您配置了SpringTempalteResolver。您唯一需要做的就是为您的自定义DbTemplateResolver 提供一个@Bean 方法,仅此而已。在要使用模板解析器的类中,只需自动连接模板解析器。不要尝试向其添加配置,因为这已经完成了。

标签: thymeleaf


【解决方案1】:

您的代码有缺陷,您似乎对 Spring 的工作原理以及应该如何使用 Spring 进行配置缺乏基本的了解。

首先,您正在使用 Spring Boot 并希望使用 Thymeleaf。这只需添加spring-boot-starter-thymeleaf 作为您项目的依赖项即可完成。 (我假设你已经这样做了)。

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

Spring Boot 检测到您的类路径中有 Thymeleaf,ThymeleafAutoConfiguration 将启动并为您配置 SpringTemplateEngine。它甚至会检测到ITemplateResolver 类型的每个bean,我假设你的DbTemplateResolver 实现了这些bean。

添加DbTemplateResolver 的唯一方法是将@Bean 方法添加到@Configuration(或您的应用程序)类。

@Bean
public ITemplateResolver dbTemplateResolver() {
    return new DbTemplateResolver();
}

Spring 会检测到它并将其注入到自动配置的SpringTemplateEngine 中。

你唯一需要做的就是在课堂上你需要SpringTemplateEngine,你需要自动连接它。只需使用超类而不是具体类型。

@Autowired
private TemplateEngine templateEngine;

不要在以后尝试配置它,使用框架。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 2015-12-09
    • 2018-07-08
    • 2017-04-05
    • 2021-12-11
    • 2017-05-07
    相关资源
    最近更新 更多