2012-08-01 10 views
17

मैं वसंत, हाइबरनेट, स्ट्रूट्स और मेवेन का उपयोग कर वेब एप्लिकेशन बना रहा हूं।स्वाभाविक निर्भरताओं का इंजेक्शन विफल रहा; नेस्टेड अपवाद है org.springframework.beans.factory.BeanCreationException:

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> 

मैं:

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 निर्भरता है कि है:

जब मैं mvn clean install आदेश चला मैं नीचे त्रुटि मिलती है Autowired निर्भर स्कैन करने के लिए मेरे संदर्भ विन्यास में जोड़ा है encies। लेकिन मुझे यकीन नहीं है कि यह अभी भी यह अपवाद क्यों दे रहा है।

मैं भी निम्नलिखित तरीके से इसे जोड़ने की कोशिश की लेकिन मैं अभी भी एक ही अपवाद

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

अद्यतन प्राप्त करें:

निम्नलिखित पासवर्ड संकेत कार्रवाई है

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 के किसी भी सेम शामिल नहीं है देखना –

+0

@Kltis: संपादित उत्तर –

+0

@Klits: मैंने अपना जवाब संपादित किया, एक नज़र डालें। –

उत्तर

21

उपयोग घटक स्कैनिंग के रूप में नीचे दी गई है, अगर com.project.action.PasswordHintAction स्टीरियोटाइप के साथ टिप्पणी की जाती है अपनी टिप्पणियों

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

संपादित

मैं आपकी समस्या को देखते हैं, PasswordHintActionTest में आप PasswordHintAction autowiring कर रहे हैं। लेकिन आपने PasswordHintAction के लिए ऑटोवायर करने के लिए बीन कॉन्फ़िगरेशन नहीं बनाया था।

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

तरह PasswordHintAction को स्टीरियोटाइप एनोटेशन (@Component, @Service, @Controller) में से एक जोड़े या एक्सएमएल विन्यास बनाने में applicationcontext.xml

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

वास्तव में यह कॉन्फ़िगरेशन किया गया था कॉन्फ़िगरेशन फ़ाइल अलग-अलग कॉन्फ़िगरेशन फ़ाइल है जिसे applicationContext-struts.xml कहा जाता है। मैंने इस अपडेट के साथ अपना प्रश्न अपडेट किया है। लेकिन इसके साथ ही मुझे यह त्रुटि मिलती है – KItis

+0

आपके उत्तर के लिए धन्यवाद, मैंने applicationContext-struts.xml को मेरी web.xml फ़ाइल में लिंक नहीं किया है। वह समस्या थी। वैसे भी आपके जवाब ने मुझे इस समाधान में शामिल होने में मदद की। – KItis

3

आपको ऑटोवायर के लिए उम्मीदवार प्रदान करने की आवश्यकता है। इसका मतलब है कि पासवर्डहिंट का एक उदाहरण वसंत के लिए जाना चाहिए ताकि यह अनुमान लगाया जा सके कि इसे संदर्भित करना चाहिए।

कृपया आगे सहायता के लिए उस श्रेणी की पासवर्डहिंट और/या वसंत बीन परिभाषा के वर्ग प्रमुख प्रदान करें।

PasswordHintAction passwordHintAction; 

इतना है कि यह सेम परिभाषा से मेल खाता है करने के लिए

PasswordHintAction action; 

का नाम बदलने की कोशिश करें।

+0

आपके प्रयास के लिए धन्यवाद, वैसे भी समस्या यह थी कि मैंने एप्लिकेशन को नहीं जोड़ा था। कॉन्टैक्स-स्ट्रैटस.एक्सएमएल विच में पासवर्ड पैरामीटर बीन परिभाषा है जो संदर्भ पैरामीटर के तहत web.xml फ़ाइल से जुड़ा हुआ है । – KItis

संबंधित मुद्दे