我正在尝试在我的应用中实现电子邮件功能,但我一直在
No matching bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
有人能指出我做错了什么吗?
bean的xml配置是:
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<context:annotation-config/>
//...other stuff
<beans:bean id="mailSession" class="org.springframework.jndi.JndiObjectFactoryBean">
<beans:property name="jndiName" value="EmailServer" />
</beans:bean>
<beans:bean id="emailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<beans:property name="session" ref="mailSession"/>
</beans:bean>
EmailServiceImpl类:
@Service
public class EmailServiceImpl implements EmailService {
@Autowired
private JavaMailSenderImpl emailSender;
//more code..
}
答案 0 :(得分:4)
我正在努力解决这个问题,因为电子邮件服务类编码如下:
@Service("emailService")
public class EmailService {
@Autowired private JavaMailSenderImpl mailSender;
...
public void send(...) {
// send logic
}
}
在阅读相关主题时,我stumbled跨解决方案。关键点是JavaMailSender
接口在applicationContext.xml
中定义为Spring JavaMailSenderImpl
类。
步骤1:修改应用程序上下文文件以包含以下bean定义:
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl"
p:host="myMailserver.mycompany.com" />
步骤2:电子邮件服务类被修改为:
@Service("emailService")
public class EmailService {
@Autowired private JavaMailSender mailSender; // Observe the change in the type
...
瞧!春天很开心。我会虽然希望听到对原始错误的正确解释。
答案 1 :(得分:2)
感谢大家的回复。我无法使自动装配工作,但我通过执行以下操作获得了整体电子邮件解决方案:
添加到servlet-context.xml:
<beans:bean id="mailSession" class="org.springframework.jndi.JndiObjectFactoryBean">
<beans:property name="jndiName" value="myMailSession" />
</beans:bean>
<beans:bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<beans:property name="session" ref="mailSession"/>
</beans:bean>
<beans:bean id="emailServiceImpl" class="com.name.here.business.EmailServiceImpl">
<beans:property name="mailSender" ref="mailSender"/>
</beans:bean>
添加到web.xml:
<resource-ref>
<description>the email session</description>
<res-ref-name>myMailSession</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>
添加到weblogic.xml:
<resource-description>
<res-ref-name>myMailSession</res-ref-name>
<jndi-name>myMailSession</jndi-name>
</resource-description>
EmailServiceImpl:
@Service
public class EmailServiceImpl implements EmailService {
private JavaMailSender mailSender;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
//..other code
}
答案 2 :(得分:1)
您需要将<context:annotation-config/>
添加到配置文件中,以便Spring自动注释带注释的bean。
http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config
答案 3 :(得分:1)
从错误消息中,我可以得出结论,自动装配正在运行,但它无法找到所需的bean。
确保加载所有bean定义文件。
答案 4 :(得分:1)
您的JavaMailSenderImpl类本身是否有@Service或类似的注释?这将导致Spring的组件扫描程序将其实例放入spring容器中,然后可以将其自动装入EmailServiceImpl。
答案 5 :(得分:1)
这就是我修复它的方法:
我也遇到了这个问题,我尝试通过手动加载app-context.xml文件来跟踪在测试过程中完美运行的简单教程,但是当我尝试运行我的spring mvc app时,它仍然显示此错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.mail.javamail.JavaMailSender] 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:952)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:821)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
... 42 more
在尝试各种各样的事情之后,我碰巧将这两行从我的JPA / DB配置文件移到了root-config文件的底部。
<context:annotation-config/>
<context:component-scan base-package="my.app.service.layer"/>
我还在学习Spring,但我认为它们出现的顺序存在问题。
修改强> 这个问题似乎澄清了订单的问题:
Difference between applicationContext.xml and spring-servlet.xml in Spring Framework