首页 文章

将spring从3.x升级到4.3.2后,JavaMailSender和VelocityEngine的自动装配失败

提问于
浏览
1

引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.springframework.mail.javamail.JavaMailSender com.xxx.service.impl.NotificationServiceImpl.mailSender;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[org.springframework.mail.javamail.JavaMailSender]的限定bean依赖:预期至少有1个bean有资格作为此依赖项的autowire候选者 . 依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

applicationContext.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:aop="http://www.springframework.org/schema/aop"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"
   default-lazy-init="true">
<!-- Enable @Transactional support -->
<tx:annotation-driven/>

<!-- Enable @AspectJ support -->
<aop:aspectj-autoproxy/>

<!-- Activates scanning of @Autowired -->
<context:annotation-config/>

<!-- Activates scanning of @Service -->
<context:component-scan base-package="com.xxx.service"/>

<bean id="userSecurityAdvice" class="com.xxx.service.UserSecurityAdvice"/>

<bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"/>

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" autowire="byName">
    <property name="host" value="${mail.host}" />
    <property name="port" value="${mail.port}" />
    <property name="username" value="${mail.username}" />
    <property name="password" value="${mail.password}" />
    <property name="javaMailProperties">
       <props>
              <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
              <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
              <prop key="mail.from">${mail.from}</prop>
           </props>
    </property>
</bean>

<!-- Configure Velocity for sending e-mail -->
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="velocityProperties">
        <props>
            <prop key="resource.loader">class</prop>
            <prop key="class.resource.loader.class">
                org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
            </prop>
            <prop key="velocimacro.library"></prop>
        </props>
    </property>
</bean>

其他依赖:

  • javax.mail - 1.4.7

  • org.apache.velocity - 1.7


package com.xxx.service.impl;

@Service("notificationService")
public class NotificationServiceImpl implements NotificationService {
    @Autowired
    private JavaMailSender mailSender;
}

所有依赖项都已正确添加, JavaMailSender class也存在于spring-context-support-xxx.jar中,但无法找到它未获得自动装配的原因 . 有人可以帮忙吗?

1 回答

  • 0

    尝试从.m2目录中删除spring文件夹 . 然后更新您的maven依赖项并重建项目 .

相关问题