Hayi
Hayi

Reputation: 6236

Could not autowire field

I'm using Hibernate 4 + Spring MVC 4 and when i start Apache Tomcat Server 8 I got this error :

Error creating bean with name 'welcome': Injection of autowired dependencies failed;
Could not autowire field: private dao.IRegion controller.welcome.regionI;
No qualifying bean of type [dao.IRegion] 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)}

here's my Hibernate Configuration which contain <property name="packagesToScan" value="dao" /> :

<?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.2.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.2.xsd">

   <context:property-placeholder location="persistence-mysql.properties" />

   <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="packagesToScan" value="dao" />
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
         </props>
      </property>
   </bean>

<!--    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> -->
   <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
      <property name="driverClassName" value="${jdbc.driverClassName}" />
      <property name="url" value="${jdbc.url}" />
      <property name="username" value="${jdbc.user}" />
      <property name="password" value="${jdbc.pass}" />
   </bean>

   <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>

   <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>

dao is the package where i put my dao and interfaces.

My region Interface dao.IRegion :

public interface IRegion<T extends Serializable> {

    List<T> findAll();

}

my Region DAO dao.RegionDAO

@Repository
public class RegionDAO  implements IRegion < Region > {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<Region> findAll() {

        return  sessionFactory.getCurrentSession().createQuery("from Region").list();
    }

}

My Controller

@Controller
public class welcome {

    @Autowired
    private IRegion<Region> regionI;
        ....

}

My servlet dispatcher

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
    <mvc:default-servlet-handler />
    <context:component-scan base-package="controller"/>
    <mvc:annotation-driven />

    <mvc:resources mapping="/resources/**" location="/resources/" />  

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>


</beans>

Upvotes: 3

Views: 25299

Answers (1)

Kevin Bowersox
Kevin Bowersox

Reputation: 94459

A bean of type RegionDao is never created by the Spring IOC Container, therefore the bean is not managed by Spring, which makes it unavailable for autowiring. Spring is basically saying, I do not have any bean that satisfies this dependency in the controller.

To make RegionDao be created and managed by Spring, component scan the class's package in the hibernate configuration file.

<context:component-scan base-package="package.with.daos"/>

This will create a bean of type RegionDao inside the Spring IOC Container and make it available for autowiring.

Upvotes: 4

Related Questions