Spring Bean自动装配错误 [英] Spring Bean Autowiring error

查看:113
本文介绍了Spring Bean自动装配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中实现电子邮件功能,但我不断收到

I am trying to implement email functionality in my app but I keep getting

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.

谁能指出我在做什么错误?

Can anyone point out what I am doing incorrectly?

bean的xml配置为:

The xml config for the bean is:

<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类:

EmailServiceImpl class:

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSenderImpl emailSender; 

    //more code..
}

推荐答案

感谢大家的回应.我无法使自动布线工作,但是通过执行以下操作,我使整个电子邮件解决方案都可以工作:

Thanks to everyone for their responses. I was unable to get the autowiring to work, but I got the overall email solution to work by doing the following:

  1. 设置 mailSession在weblogic,名称为"myMailSession"
  1. setup the mailSession in weblogic, with a jndi name of "myMailSession"

添加到servlet-context.xml:

add to 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:

add to 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:

add to weblogic.xml:

<resource-description>
  <res-ref-name>myMailSession</res-ref-name>
  <jndi-name>myMailSession</jndi-name>
</resource-description>

EmailServiceImpl:

EmailServiceImpl:

@Service
public class EmailServiceImpl implements EmailService {

    private JavaMailSender mailSender;

    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }
    //..other code
}

这篇关于Spring Bean自动装配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆