[英]Spring Bean Autowiring error
我正在嘗試在我的應用程序中實現電子郵件功能,但我一直在努力
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..
}
對於一個編碼如下的電子郵件服務類,我正在努力解決這個問題:
@Service("emailService")
public class EmailService {
@Autowired private JavaMailSenderImpl mailSender;
...
public void send(...) {
// send logic
}
}
在閱讀相關主題時,我偶然發現了一個解決方案。 關鍵點在於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
...
瞧! 春天很開心。 我想聽聽原始錯誤的正確解釋。
感謝大家的回復。 我無法使自動裝配工作,但我通過執行以下操作獲得了整體電子郵件解決方案:
添加到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
}
這就是我修復它的方法:
我也遇到了這個問題,我嘗試通過手動加載app-context.xml文件來跟蹤在測試期間完美運行的簡單教程,但是當我嘗試運行我的spring mvc應用程序時,它仍然顯示此錯誤:
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,但我認為它們出現的順序存在問題。
編輯:這個問題似乎澄清了訂單的問題:
Spring Framework中applicationContext.xml和spring-servlet.xml之間的區別
您需要將<context:annotation-config/>
到配置文件中,以便Spring自動注釋帶注釋的bean。
http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config
從錯誤消息,我可以得出結論,自動裝配正在工作,但它無法找到所需的bean。
確保加載所有bean定義文件。
您的JavaMailSenderImpl類本身是否有@Service或類似的注釋? 這將導致Spring的組件掃描程序將其實例放入spring容器中,然后可以將其自動裝入EmailServiceImpl。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.