2012-08-14 84 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。