2010-07-19 18 views
7

मेरा ऐप-config.xml मेरी UserDao सेम के लिए एक परिभाषा है है:क्षेत्र autowire नहीं किया जा सका, लेकिन मैं परिभाषा

<bean id="userDao" class="com.blah.core.db.hibernate.UserDaoImpl"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 

मैं अपने घटक स्कैनिंग है:

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

मेरे सूचकांक कार्रवाई मेरे होमकंट्रोलर में ठीक काम करता है (यह मेरे उपयोगकर्ता सेवा पर एक freemarker टेम्पलेट पर एक विधि की सामग्री outputs)।

@Controller 
public class HomeController { 

    @Autowired 
    private UserService userService; 




    @RequestMapping("/") 
    public ModelAndView Index() { 



     ModelAndView mav = new ModelAndView(); 

     mav.setViewName("index"); 
     mav.addObject("message", userService.sayHello()); 

     mav.addObject("username", userService.getTestUser()); 


     return mav; 
    } 

'getTestUser()', एक नई पद्धति है कि UserDao लिए संदर्भ बनाता है यह दिखाई देता है:

@Service 
public class UserServiceImpl implements UserService{ 

    @Autowired 
    UserDao userDao; 

    public String sayHello() { 

     return "hello from user service impl part 2"; 

    } 

    public String getTestUser() { 


     return userDao.getById(1L).getUsername(); 

    } 


} 

मैं त्रुटि हो रही है:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.blah.core.db.hibernate.UserDao com.blah.core.services.UserServiceImpl.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.blah.core.db.hibernate.UserDao] 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)} 
  1. क्या क्या मुद्दा हो सकता है?
  2. अगर मैंने ऑटोवायर नहीं किया है, तो मैं उपयोगकर्ताडाओ परिभाषा पर @AutoWire जोड़ने के बजाय क्या करूँगा।

उत्तर

15

आप वसंत के सभी पंजीकृत सेम निर्यात करने की कोशिश की पता लगाने के लिए अगर userDao सेम सूची में है है (debugging के माध्यम से स्मृति या Spring's bootstrap log पढ़ने के लिए या)। कृपया सुनिश्चित करें कि UserDaoImpl वास्तव में UserDao लागू कर रहा है - मैं इस पर इशारा कर रहा हूं क्योंकि यहां UserDaoImpl का स्निपेट नहीं देख रहा है।

आप @Autowired का उपयोग नहीं करते हैं, तो विकल्प स्पष्ट रूप से ApplicationContext के getBean() के माध्यम से फलियों का एक संदर्भ प्राप्त करने के लिए किया जाएगा (ठीक जिसे एक गंदे तरीके के रूप में माना जाता है, अपने @Autowired बजाय), अपने सेम नाम, वर्ग के नाम, आदि के माध्यम से

+0

ठीक है मैं उपयोगकर्ताडाओ आह लागू नहीं कर रहा था! धन्यवाद! बीटीडब्ल्यू, आप वसंत के बूटस्ट्रैप लॉग या डिबगिंग के माध्यम से मेमोरी कैसे पढ़ते हैं?) वे अच्छे कौशल हैं जिन्हें मुझे सीखना चाहिए! – Blankman

+0

मैंने लॉगिंग और डिबगिंग के संदर्भों के साथ उत्तर अद्यतन किया है। :) – yclian

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