【发布时间】: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方法并简单地删除@AutowireMessageFormatHelper中的模板解析器......我从来没有说过你应该改变它! -
No... 如前所述删除
@Bean方法,spring boot 在检测到百里香叶时已经为您配置了SpringTempalteResolver。您唯一需要做的就是为您的自定义DbTemplateResolver提供一个@Bean方法,仅此而已。在要使用模板解析器的类中,只需自动连接模板解析器。不要尝试向其添加配置,因为这已经完成了。
标签: thymeleaf