2012-08-01 13 views
17

Tworzę aplikację internetową przy użyciu Spring, Hibernate, Struts i Maven.Wstrzyknięcie autowyred zależności nie powiodło się; Zagnieżdżony wyjątek to org.springframework.beans.factory.BeanCreationException:

otrzymuję poniżej błąd podczas uruchamiania mvn clean install polecenie:

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

Poniżej znajduje się klasa, która posiada zależność 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")); 
    } 


} 

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

I dodano do mojej konfiguracji kontekstowej, aby skanować zależne od Autowyred encies. Ale nie jestem pewien, dlaczego wciąż daje ten wyjątek.

Próbowałem dodanie go w następujący sposób także, ale wciąż ten sam wyjątek

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

UPDATE:

Poniżej znajduje działaniem wskazówkę dotyczącą hasła

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

Aktualizacja 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

widzę tylko część pliku kontekście, który nie zawiera żadnych fasoli typu PasswordHintAction –

+0

@Kltis: edytowana odpowiedź –

+0

@Klits: Edytowałem swoją odpowiedź, spójrz. –

Odpowiedz

21

Zastosowanie jak podano poniżej, jeśli com.project.action.PasswordHintAction jest opatrzone stereotypu adnotacje

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

EDIT

widzę problemu, w PasswordHintActionTest jesteś autowiring PasswordHintAction. Ale nie utworzyłeś konfiguracji komponentu bean dla PasswordHintAction do automatycznego wypromowania.Dodaj jedną z adnotacji stereotypu (@Component, @Service, @Controller) do PasswordHintAction jak

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

lub utworzyć konfigurację xml w applicationcontext.xml jak

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

faktycznie miałem tej konfiguracji jest oddzielny plik konfiguracyjny o nazwie applicationContext-struts.xml. Mam zaktualizowane moje pytanie z tą aktualizacją. ale nawet z tym otrzymałem ten błąd – KItis

+0

dziękuję za odpowiedź, nie połączyłem applicationContext-struts.xml do mojego pliku web.xml. to był problem. tak czy inaczej twoja odpowiedź pomogła mi znaleźć to rozwiązanie. – KItis

3

Musisz podać kandydata na autora. Oznacza to, że instancja haseł PasswordHint musi być znana z tego, że wypływa w taki sposób, że może odgadnąć, że musi się do niej odwoływać.

Proszę podać nagłówek klasy PasswordHint i/lub definicję sprężyny ziaren dla tej klasy, aby uzyskać dalszą pomoc.

Spróbuj zmienić nazwę

PasswordHintAction action; 

do

PasswordHintAction passwordHintAction; 

aby odpowiadała definicji fasoli. Skanowanie komponent

+0

dzięki za wysiłek, w każdym razie problem nie został dodany applicationContaxt-strtus.xml wihch ma definicję fasoli hodującej hasła, powiązaną z plikiem web.xml w kontekście parametrów kontekstowych . – KItis

Powiązane problemy