1

I have browsed many related topics and tried different ways, but the error is still there. Environment: Spring data jpa, Hibernate, maven, tomcat7

Error Stack:

org.apache.catalina.core.StandardContext listenerStart SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.zzz.service.UserService com.zzz.controller.UserController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.zzz.service.UserService] 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)} at ...

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" ... ... >
<context:component-scan base-package="com.zzz">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/zzz" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan" value="com.zzz.entity" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        </props>
    </property>
    <property name="persistenceProvider">
        <bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
    </property>
</bean>

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

 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="dataSource" ref="dataSource" />
 </bean>

 <jpa:repositories base-package="com.zzz.repository" />

dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" ... ... >
<context:component-scan base-package="com.zzz.controller" />
<mvc:annotation-driven />
<mvc:resources mapping="/static/**" location="static/" />

<bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/defs/templates.xml</value>
        </list>
    </property>
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.tiles3.TilesView" />
</bean>
</beans>

UserController.java

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/users")
    public String users(Model model) {
        model.addAttribute("users", userService.findAll());
        return "users";
    }
}

UserService.java

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public List<User> findAll() {
        return userRepository.findAll();
    }
}

UserRepository.java

public interface UserRepository extends JpaRepository<User, Integer> {

}

UDPATE 1

There are 2 autowired errors:

Could not autowire field: private com.zzz.repository.UserRepository com.zzz.service.UserService.userRepository

could not autowire field: private com.zzz.service.UserService com.zzz.controller.UserController.userService

UPDATE 2

I've tried to use servlet-dispatcher.xml as the only configuration file (put the content of applicationContext.xml into it), it works fine. So it's the problem between these 2 xml files, but I couldn't tell it.

UPDATE 3

I renamed applicationContext.xml to root-context.xml, and set the contextConfigLocation to it in web.xml, the error disappeared!

It seems to be some configuration error about the applicationContext.xml, anybody could help?

1 Answers1

0

another guess: in the dispatcher xml you only scan for the controller package...so if the application.xml is not loaded it does not scan the service package and thus the user service is not loaded... would be interesting what happens if you scan for the entire com.zzz package in the dispatcher.xml

Ueli Hofstetter
  • 2,409
  • 4
  • 29
  • 52