2012-08-14 87 views
25

我一直在收到這個錯誤,不知道爲什麼。是的,我知道有很多人有類似的問題,但閱讀他們得到的答案,並不能解決我的問題。春天無法自動裝配字段。爲什麼?

org.springframework.beans.factory.BeanCreationException:創建名爲'contactController'的bean時出錯:注入自動裝配依賴項失敗;嵌套異常是org.springframework.beans.factory.BeanCreationException:無法自動裝入字段:private net.service.ContactService net.controller.ContactController.contactService;嵌套的異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到匹配的[net.service.ContactService]類型的bean用於依賴性:期望至少1個符合此依賴性的autowire候選者。依賴註解:{@ org.springframework.beans.factory.annotation.Autowired(所需=真)}

這裏是控制器:

@Controller 
@SessionAttributes 
public class ContactController { 

    @Autowired 
    private ContactService contactService; 
//methods... 


} 

的ContactServiceImpl

@Service("contactService") 
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true) 
public class ContactServiceImpl implements ContactService { 

    @Autowired 
    private ContactDao contactDao; 

    public ContactServiceImpl() { 
    } 

    @Override 
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false) 
    public void addContact(Contact contact) { 
     contactDao.saveContact(contact); 
    } 

    @Override 
    public List<Contact> getContacts() { 
     return contactDao.getAllContacts(); 
    } 

} 

ContactDaoImpl

@Repository("contactDao") 
public class ContactDaoImpl implements ContactDao { 

    @Autowired 
    private SessionFactory sessionFactory; 

    @Override 
    public void saveContact(Contact contact) { 
     sessionFactory.getCurrentSession().saveOrUpdate(contact); 
    } 

    @Override 
    @SuppressWarnings("unchecked") 
    public List<Contact> getAllContacts() { 
     return (List<Contact>) sessionFactory.getCurrentSession().createQuery("from contact c").list(); 
    } 

} 

和彈簧servlet.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" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    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/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 

    <context:property-placeholder location="classpath:jdbc.properties" /> 
    <context:component-scan base-package="net.controller" /> 


    <tx:annotation-driven transaction-manager="hibernateTransactionManager" /> 

    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass" 
      value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${database.driver}" /> 
     <property name="url" value="${database.url}" /> 
     <property name="username" value="${database.user}" /> 
     <property name="password" value="${database.password}" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="annotatedClasses"> 
      <list> 
       <value>net.form.Contact</value> 
      </list> 
     </property> 


     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="hibernateTransactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
</beans> 
+0

那麼你有'contactService'字段的setter方法嗎? – sundar 2012-08-14 14:29:17

+0

@sundar爲了使Spring注入工作,您不需要該字段的setter方法。無論如何,這裏的問題顯然是這樣的事實,就像Spring知道的那樣,注入的bean沒有被發現,並不是它被發現,但它無法注入它。 – 2012-08-14 14:35:34

+1

@DaveNewton:呃,你說得對,我寫了一個奇怪的長答案,根本沒有提到這一點。是的,如果包含ContactServiceImpl的包不在註冊爲可註釋的那些包中,那麼該bean將不會被創建,因此不能用於其他bean中的注入 – 2012-08-14 14:37:16

回答

33

在彈簧的servlet的.xml:

<context:component-scan base-package="net.controller" /> 

(我假設服務IMPL是在同一個包的服務接口「net.service」)

我認爲你必須包net.service(或全部淨)添加到組件掃描。目前spring只在net.controller中搜索組件,而作爲你的服務impl在net.service中,它不會被spring實例化。

+0

評論之前閱讀了答案,是的,這是一個好主意。 2012-08-14 14:41:14

+0

我錯誤地將服務保存到其他包中,它工作得很好。謝謝。 – 2015-08-05 12:05:05

8

那麼有與創作ContactServiceImpl豆的問題。首先,確保在啓動Spring上下文時以及創建ContactController的實例時,通過調試無參數構造函數實際實例化該類。

如果ContactServiceImpl實際上是由Spring上下文實例化的,但它與您的@Autowire註釋根本沒有匹配,請嘗試在註釋注入中更明確。下面是處理類似的問題,因爲你這樣的傢伙,給一些可能的解決方案:

http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/

如果你問我,我想,如果你更換

@Autowired 
private ContactService contactService; 

你會沒事:

@Resource 
@Qualifier("contactService") 
private ContactService contactService; 
9

我得到這個相同的錯誤,並尋找它導致我在這裏。我的修復似乎只是將@Component註釋添加到抽象服務的實現中。

在這種情況下,這將是這樣的:

import org.springframework.stereotype.Component; 

... 

@Component 
public class ContactServiceImpl implements ContactService { 
0

我有完全相同的問題 嘗試把兩個類在同一個包,並在pom.xml添加行

<dependency> 
      <groupId> org.springframework.boot </groupId> 
      <artifactId> spring-boot-starter-web </artifactId> 
      <version> 1.2.0.RELEASE </version> 
</dependency> 
5

當你得到這個錯誤時,缺少一些註解。 我在服務上缺少@service註釋。當我添加註釋時,它對我來說工作得很好。

0

在java中配置,請確保您有導入配置在RootConfig這樣 @Import(PersistenceJPAConfig.class)

1

我今天所面臨的同樣的問題。原來我忘了提及服務實現文件的@Service/@ Component註釋,對於這個註釋,spring不能自動裝載並且無法創建bean。