2011-12-27 107 views
6

我想在我的應用程序來實現電子郵件功能,但我不斷收到的Spring bean自動裝配誤差

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. 

任何人都可以指出我在做什麼錯誤?

XML配置的bean是:

<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.. 
} 

project_structure

回答

2

感謝大家的迴應。我無法獲得自動裝配工作,但我得到了全面的電子郵件解決方案通過執行以下工作:在weblogic

  1. setup的郵件會話,用「myMailSession」

的JNDI名稱增加的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 
} 
+0

我遇到了同樣的問題,並且在xml中配置時一切正常。從服務bean emailServiceImpl中刪除autowired和@service註釋。自動裝配在其他豆類中工作正常。我想知道JavaMail中的自動裝配有什麼問題。 – 2016-01-26 14:38:49

1

您需要添加<context:annotation-config/>到您的配置文件,來讓Spring自動裝配註解豆。

http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config

+0

我試過了,它給出了同樣的錯誤。我會將其添加回原始帖子。任何其他想法? – flynfish 2011-12-27 17:59:45

+0

還有兩件事你可以嘗試。在xml中添加另一行以指定掃描您的bean的位置\t 並嘗試在電子郵件發件人bean上將autowire-candidate屬性設置爲true。 – Travis 2011-12-27 18:03:49

+0

我能夠重現您的錯誤,組件掃描應該可以解決您的問題。 – Travis 2011-12-27 18:21:30

1

從錯誤信息,我可以斷定,自動裝配工作,但它無法找到所需的豆。

確保您加載了所有的bean定義文件。

1

您的JavaMailSenderImpl類本身是否有@Service或類似的註釋?這將導致Spring的組件掃描器將其實例放入spring容器中,然後它可以自動裝載到EmailServiceImpl上。

1

這是我如何固定它:

我就遇到了這個問題也一樣,我試圖按照簡單教程通過手動加載app-context.xml文件,在測試期間完美工作的在線,但是當我試圖運行我的彈簧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配置文件的底部。

<context:annotation-config/>   
<context:component-scan base-package="my.app.service.layer"/> 

我還在學習Spring,但我在考慮他們出現的順序有問題。

編輯: 這個問題似乎澄清與秩序問題:

Difference between applicationContext.xml and spring-servlet.xml in Spring Framework

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 
    ... 

瞧!春天很開心。我想雖然喜歡聽到原始錯誤的正確解釋。