繁体   English   中英

Spring Bean自动装配错误

[英]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..
}

project_structure

对于一个编码如下的电子邮件服务类,我正在努力解决这个问题:

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

瞧! 春天很开心。 听听原始错误的正确解释。

感谢大家的回复。 我无法使自动装配工作,但我通过执行以下操作获得了整体电子邮件解决方案:

  1. 在weblogic中设置 mailSession,jndi名称为“myMailSession”

添加到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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM