2012-08-01 57 views
17

我正在使用Spring,Hibernate,Struts和Maven創建Web應用程序。自動佈線依賴項的注入失敗;嵌套異常是org.springframework.beans.factory.BeanCreationException:

我得到下面的錯誤,當我運行mvn clean install命令:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.project.action.PasswordHintActionTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.project.action.PasswordHintAction com.project.action.PasswordHintActionTest.action; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.project.action.PasswordHintAction] 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)} 

以下是具有自動裝配Autowired依賴類:

import com.opensymphony.xwork2.Action; 
import org.project.model.User; 
import org.proejct.service.UserManager; 
import org.junit.Test; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.subethamail.wiser.Wiser; 

import static org.junit.Assert.*; 
public class PasswordHintActionTest extends BaseActionTestCase { 
    @Autowired 
    private PasswordHintAction action; 
    @Autowired 
    private UserManager userManager; 

    @Test 
    public void testExecute() throws Exception { 
     // start SMTP Server 
     Wiser wiser = new Wiser(); 
     wiser.setPort(getSmtpPort()); 
     wiser.start(); 

     action.setUsername("user"); 
     assertEquals("success", action.execute()); 
     assertFalse(action.hasActionErrors()); 

     // verify an account information e-mail was sent 
     wiser.stop(); 
     assertTrue(wiser.getMessages().size() == 1); 

     // verify that success messages are in the request 
     assertNotNull(action.getSession().getAttribute("messages")); 
    } 


} 

applicationcontext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     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.xsd" 
     default-lazy-init="true"> 

    <!-- Activates scanning of @Autowired --> 
    <context:annotation-config/> 

    <!-- Activates scanning of @Repository and @Service --> 
    <context:component-scan base-package="com.project"/> 

    <!-- Compass Search Section --> 
    <!-- Compass Bean, automatically scanning for searchable classes within the model --> 
    <!-- Hooks into Spring transaction management and stores the index on the file system --> 
    <bean id="compass" class="org.compass.spring.LocalCompassBean"> 
     <property name="mappingScan" value="org.project"/> 
     <property name="postProcessor" ref="compassPostProcessor"/> 
     <property name="transactionManager" ref="transactionManager" /> 
     <property name="settings"> 
      <map> 
       <entry key="compass.engine.connection" value="target/test-index" /> 
      </map> 
     </property> 
    </bean> 

我已添加到我的上下文配置以掃描Autowired depends encies。但我不知道爲什麼它仍然會給這個例外。

我試着在下面的方法添加它也,但我仍然得到同樣的異常

<context:component-scan base-package="com.project.*"/> 

UPDATE:

以下是密碼提示作用

import org.project.model.User; 
import com.project.webapp.util.RequestUtil; 
import org.springframework.mail.MailException; 
import org.springframework.security.core.userdetails.UsernameNotFoundException; 

import java.util.ArrayList; 
import java.util.List; 


public class PasswordHintAction extends BaseAction { 
    private static final long serialVersionUID = -4037514607101222025L; 
    private String username; 

    /** 
    * @param username The username to set. 
    */ 
    public void setUsername(String username) { 
     this.username = username; 
    } 

    /** 
    * Execute sending the password hint via e-mail. 
    * 
    * @return success if username works, input if not 
    */ 
    public String execute() { 
     List<Object> args = new ArrayList<Object>(); 

     // ensure that the username has been sent 
     if (username == null) { 
      log.warn("Username not specified, notifying user that it's a required field."); 

      args.add(getText("user.username")); 
      addActionError(getText("errors.requiredField", args)); 
      return INPUT; 
     } 

     if (log.isDebugEnabled()) { 
      log.debug("Processing Password Hint..."); 
     } 

     // look up the user's information 
     try { 
      User user = userManager.getUserByUsername(username); 
      String hint = user.getPasswordHint(); 

      if (hint == null || hint.trim().equals("")) { 
       log.warn("User '" + username + "' found, but no password hint exists."); 
       addActionError(getText("login.passwordHint.missing")); 
       return INPUT; 
      } 

      StringBuffer msg = new StringBuffer(); 
      msg.append("Your password hint is: ").append(hint); 
      msg.append("\n\nLogin at: ").append(RequestUtil.getAppURL(getRequest())); 

      mailMessage.setTo(user.getEmail()); 
      String subject = '[' + getText("webapp.name") + "] " + getText("user.passwordHint"); 
      mailMessage.setSubject(subject); 
      mailMessage.setText(msg.toString()); 
      mailEngine.send(mailMessage); 

      args.add(username); 
      args.add(user.getEmail()); 

      saveMessage(getText("login.passwordHint.sent", args)); 
     } catch (UsernameNotFoundException e) { 
      log.warn(e.getMessage()); 
      args.add(username); 
      addActionError(getText("login.passwordHint.error", args)); 
      getSession().setAttribute("errors", getActionErrors()); 
      return INPUT; 
     } catch (MailException me) { 
      addActionError(me.getCause().getLocalizedMessage()); 
      getSession().setAttribute("errors", getActionErrors()); 
      return INPUT; 
     } 

     return SUCCESS; 
    } 
} 

更新2:

applicationContext-struts.xml:

<bean id="passwordHintAction" class="com.project.action.PasswordHintAction" scope="prototype"> 
    <property name="userManager" ref="userManager"/> 
    <property name="mailEngine" ref="mailEngine"/> 
    <property name="mailMessage" ref="mailMessage"/> 
</bean> 
+0

我只看到你的背景文件的一部分,其中不包含類型PasswordHintAction的任何豆 – 2012-08-01 12:24:16

+0

@Kltis:編輯答案 – 2012-08-01 12:55:07

+0

@Klits:我編輯了我的答案,看看。 – 2012-08-01 13:45:49

回答

21

使用組件掃描註解

<context:component-scan base-package="com.project.action"/> 

編輯

我看到你的問題,在PasswordHintActionTest你自動裝配PasswordHintAction。但是您沒有爲PasswordHintAction創建自動裝配的bean配置。加入刻板印象註釋(@Component, @Service, @Controller)之一PasswordHintAction

@Component 
public class PasswordHintAction extends BaseAction { 
    private static final long serialVersionUID = -4037514607101222025L; 
    private String username; 

或創建XML配置在applicationcontext.xml

<bean id="passwordHintAction" class="com.project.action.PasswordHintAction" /> 
+2

實際上,我曾將這個配置稱爲applicationContext-struts.xml,它是單獨的配置文件。我已更新我的問題與此更新。但即使如此,我得到這個錯誤 – KItis 2012-08-01 13:04:06

+0

感謝您的回答,我還沒有將applicationContext-struts.xml鏈接到我的web.xml文件。這是問題所在。無論如何,你的回答幫助我進入這個解決方案。 – KItis 2012-08-01 13:57:30

3

您需要提供autowire的候選人。這意味着必須知道PasswordHint的一個實例,以便可以猜測它必須引用它。

請提供PasswordHint的類頭和/或該類的spring bean定義以獲得進一步的幫助。

嘗試改變的

PasswordHintAction action; 

名稱

PasswordHintAction passwordHintAction; 

,使其bean定義相匹配。如下面給出的,如果com.project.action.PasswordHintAction與刻板印象註釋

+0

感謝您的努力,無論如何問題是我沒有添加applicationContaxt-strtus.xml wihch有密碼管理bean定義鏈接到web.xml文件在上下文參數。 – KItis 2012-08-01 13:58:43

相關問題