Thymeleaf-Spring4 无法自动装配 TemplateEngine [英] Thymeleaf-Spring4 unable to autowire TemplateEngine

查看:53
本文介绍了Thymeleaf-Spring4 无法自动装配 TemplateEngine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Thymeleaf 电子邮件模板添加到正在运行的 Spring MVC + Thymeleaf 应用程序中,如here.

当我尝试在我的服务类中自动装配 TemplateEngine 时收到此错误:

错误:org.springframework.web.context.ContextLoader - 上下文初始化失败org.springframework.beans.factory.BeanCreationException:创建名为mailManager"的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.thymeleaf.TemplateEngine no.test.lab.manager.MailManager.templateEngine;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 找不到类型为 [org.thymeleaf.TemplateEngine] 的合格 bean 依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)在 org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)在 org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)在 org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)在 org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797)在 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)在 org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)在 org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)在 org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)在 org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)在 org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1637)在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)在 java.util.concurrent.FutureTask.run(FutureTask.java:166)在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)在 java.lang.Thread.run(Thread.java:722)引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.thymeleaf.TemplateEngine no.test.lab.manager.MailManager.templateEngine;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 找不到类型为 [org.thymeleaf.TemplateEngine] 的合格 bean 依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)……还有 27 个由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为 [org.thymeleaf.TemplateEngine] 的合格 bean 依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)... 29 更多2014 年 6 月 1 日上午 8:15:01 org.apache.catalina.core.StandardContext listenerStart严重:异常将上下文初始化事件发送到类 org.springframework.web.context.ContextLoaderListener 的侦听器实例org.springframework.beans.factory.BeanCreationException:创建名为mailManager"的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.thymeleaf.TemplateEngine no.test.lab.manager.MailManager.templateEngine;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 找不到类型为 [org.thymeleaf.TemplateEngine] 的合格 bean 依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)在 org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)在 org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)在 org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)在 org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797)在 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)在 org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)在 org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)在 org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)在 org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)在 org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1637)在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)在 java.util.concurrent.FutureTask.run(FutureTask.java:166)在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)在 java.lang.Thread.run(Thread.java:722)引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.thymeleaf.TemplateEngine no.test.lab.manager.MailManager.templateEngine;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 找不到类型为 [org.thymeleaf.TemplateEngine] 的合格 bean 依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)……还有 27 个由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为 [org.thymeleaf.TemplateEngine] 的合格 bean 依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)... 29 更多

mvc-dispatcher-servlet.xml:

<列表><value>WEB-INF\conf\config_${spring.profiles.active}.properties</value><value>WEB-INF\conf\version.properties</value></list></属性><property name="ignoreUnresolvablePlaceholders" value="true"/></bean><!-- BEGIN JSP/Tiles --><bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"><属性名称="viewClass"><value>org.springframework.web.servlet.view.tiles2.TilesView</value></属性><property name="order" value="1"/></bean><bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"><属性名称=定义"><列表><value>/WEB-INF/tiles.xml</value></list></属性></bean><!-- 结束 JSP/Tiles --><!-- BEGIN Thymeleaf --><bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"><property name="prefix" value="/WEB-INF/views/"/><property name="suffix" value=".html"/><property name="templateMode" value="HTML5"/><property name="cacheTTLMs" value="${thymeleaf.cache_ttl}"/><property name="order" value="0"/></bean><!-- THYMELEAF:电子邮件模板的模板解析器 --><bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver"><property name="prefix" value="/WEB-INF/mail/"/><property name="suffix" value=".html"/><property name="templateMode" value="HTML5"/><property name="characterEncoding" value="UTF-8"/><property name="cacheTTLMs" value="${thymeleaf.cache_ttl}"/><property name="order" value="2"/></bean><bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"><property name="templateResolvers"><设置><ref bean="templateResolver"/><ref bean="emailTemplateResolver"/></set></属性><属性名称="additionalDialects"><设置><bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/></set></属性></bean><bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver"><property name="templateEngine" ref="templateEngine"/><property name="viewNames" value="view_*"/><property name="order" value="0"/></bean><!-- END 百里香叶--><mvc:拦截器><mvc:拦截器><mvc:mapping path="/**"/><bean class="no.test.lab.web.interceptor.RootInterceptor"/></mvc:拦截器><mvc:拦截器><mvc:mapping path="/lab/**"/><bean class="no.test.lab.web.interceptor.LabInterceptor"/></mvc:拦截器></mvc:拦截器></豆类>

pom.xml:

<依赖><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring4</artifactId><version>2.1.3.RELEASE</version></依赖><依赖><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity3</artifactId><version>2.1.1.RELEASE</version></依赖>

服务类:

<前>@服务公共类邮件管理器{@自动连线私有模板引擎模板引擎;@自动连线私有 JavaMailSender mailSender;...

解决方案

您需要将 templateEngine 移动到根应用程序上下文,因为这是您的 MailManager 定义的地方.您当前配置 Spring 的方式,templateEngine 是在 web 应用程序上下文中定义的,这意味着来自根上下文的 bean 不能使用它.

为了做到这一点,只需将 templateEngine bean 及其依赖项移动到 Spring 配置文件中,您已在其中为 controller 以外的包启用了组件扫描.

I'm trying to add Thymeleaf email templates to a working Spring MVC + Thymeleaf application as described here.

I receive this error when I try to autowire the TemplateEngine in my service class:

ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.thymeleaf.TemplateEngine no.test.lab.manager.MailManager.templateEngine; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.thymeleaf.TemplateEngine] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1637)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.thymeleaf.TemplateEngine no.test.lab.manager.MailManager.templateEngine; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.thymeleaf.TemplateEngine] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 27 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.thymeleaf.TemplateEngine] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    ... 29 more
jun 01, 2014 8:15:01 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.thymeleaf.TemplateEngine no.test.lab.manager.MailManager.templateEngine; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.thymeleaf.TemplateEngine] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1637)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.thymeleaf.TemplateEngine no.test.lab.manager.MailManager.templateEngine; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.thymeleaf.TemplateEngine] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 27 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.thymeleaf.TemplateEngine] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    ... 29 more

mvc-dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="no.test.lab.web.controller" /> 

    <tx:annotation-driven/>

    <mvc:annotation-driven conversion-service="conversionService" />

    <mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
    <mvc:resources mapping="/images/**" location="/WEB-INF/images/" />
    <mvc:resources mapping="/lib/**" location="/WEB-INF/lib/" />
    <mvc:resources mapping="/assets/**" location="/WEB-INF/views/assets/" />

    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="formatters">
            <set>
                <bean class="no.test.lab.web.formatter.DateFormatter"/>
            </set>
        </property>
    </bean>

    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">  
        <property name="locations">    
            <list>      
                <value>WEB-INF\conf\config_${spring.profiles.active}.properties</value>    
                <value>WEB-INF\conf\version.properties</value>
            </list>  
        </property>  
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>     

    <!-- BEGIN JSP/Tiles -->

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">    
        <property name="viewClass">        
            <value>org.springframework.web.servlet.view.tiles2.TilesView</value>    
        </property>
        <property name="order" value="1" />
    </bean>

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">    
        <property name="definitions">        
            <list>            
                <value>/WEB-INF/tiles.xml</value>        
            </list>    
        </property>
    </bean>

    <!-- END JSP/Tiles -->

    <!-- BEGIN Thymeleaf -->
    <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5" />
        <property name="cacheTTLMs" value="${thymeleaf.cache_ttl}" />
        <property name="order" value="0" />
    </bean>

    <!-- THYMELEAF: Template Resolver for email templates -->
    <bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
        <property name="prefix" value="/WEB-INF/mail/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5" />
        <property name="characterEncoding" value="UTF-8" />
        <property name="cacheTTLMs" value="${thymeleaf.cache_ttl}" />
        <property name="order" value="2" />
    </bean>

    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">  
        <property name="templateResolvers">
            <set>
                <ref bean="templateResolver"/>
                <ref bean="emailTemplateResolver"/>
            </set>
        </property>
        <property name="additionalDialects">
            <set>
                <bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
            </set>
        </property>
    </bean>

    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="viewNames" value="view_*" />
        <property name="order" value="0" />
    </bean>    

    <!-- END Thymeleaf -->

    <mvc:interceptors>  
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="no.test.lab.web.interceptor.RootInterceptor" />
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/lab/**"/>
            <bean class="no.test.lab.web.interceptor.LabInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>

</beans>

pom.xml:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring4</artifactId>
    <version>2.1.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity3</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

Service class:


    @Service
    public class MailManager {

        @Autowired
        private TemplateEngine templateEngine;

        @Autowired
        private JavaMailSender mailSender;

        ... 

解决方案

You need to move the templateEngine to the root application context since that is where your MailManager is defined. The way you have currently configured Spring, templateEngine is defined in the web application context which means that beans from the root context cannot use it.

In order to do that just move the templateEngine bean along with it's dependencies to the Spring configuration file where you have enabled component scanning for the packages other than controller.

这篇关于Thymeleaf-Spring4 无法自动装配 TemplateEngine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆