发送电子邮件时出现NoSuchBeanDefinitionException

时间:2015-12-22 14:12:06

标签: java spring javamail spring-ioc

我尝试使用Spring管理的服务通过Restful Web-Service发送电子邮件。但是,我检索以下异常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'messsageservice' is defined
    org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
    org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1175)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
    com.hope.mobilehope.controller.Authcodegen.getactivationkey(Authcodegen.java:26)

代码:

@Service("messsageservice")

public class Messsageservice {


    @Autowired
    private MailSender mailSender;
    public void sendMail(String from, String to, String subject, String body) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(body);
        mailSender.send(message);           
    }                       
}        

@RestController    
public class Authcodegen {  

 @RequestMapping(value = "/activationkeygenerator", method = RequestMethod.GET,headers="Accept=application/json")
 public Authcode getactivationkey( @RequestParam(value="email") String name){
     Authcode authcode = new Authcode();
     Random rnd = new Random();
     ApplicationContext context = new FileSystemXmlApplicationContext("**/springrest-servlet.xml");
     Messsageservice msgservice = (Messsageservice) context.getBean("messsageservice");
     String to_email=name;
     int n = 100000 + rnd.nextInt(900000);
     String code=String.valueOf(n);
     msgservice.sendMail("ethonnoreply@gmail.com", to_email, "authcode", code);
     authcode.setId(n);
     authcode.setStatus("Sucess");
 return authcode;

  }       
}    

的pom.xml

 <dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-databind</artifactId>  
             <version>2.6.3</version>  
        </dependency>  

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.2.3.RELEASE</version>
</dependency>

这是我的Spring .xml文件:

<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  

 <mvc:annotation-driven/>  
<context:component-scan base-package="com.hope.mobilehope.controller" />  

<context:component-scan base-package="com.hope.mobilehope.common"/>



  <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.gmail.com"/>
        <property name="port" value="587"/>
        <property name="username" value="ethonnoreply@gmail.com"/>
        <property name="password" value="ethon123"/>
        <property name="javaMailProperties">
            <props>
                <!-- Use SMTP transport protocol -->
                <prop key="mail.transport.protocol">smtp</prop>
                <!-- Use SMTP-AUTH to authenticate to SMTP server -->
                <prop key="mail.smtp.auth">true</prop>
                <!-- Use TLS to encrypt communication with SMTP server -->
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.debug">true</prop>
            </props>
        </property>
    </bean>


</beans>  

1 个答案:

答案 0 :(得分:0)

尝试在文件messsageservice中创建bean spring.xml,如下所示:

  <bean id="messsageservice" class="com.hope.mobilehope.common.Messsageservice">
    ...
   </bean>