org.springframework.beans.factory.BeanCreationException:使用名称' userController'创建bean时出错:注入自动连接的依赖项失败

时间:2015-03-24 21:10:37

标签: java spring hibernate maven jpa

我浏览了许多相关主题并尝试了不同的方法,但错误仍然存​​在。环境:Spring数据jpa,Hibernate,maven,tomcat7

错误堆栈:

  

org.apache.catalina.core.StandardContext listenerStart   SEVERE:将上下文初始化事件发送到类org.springframework.web.context.ContextLoaderListener的侦听器实例的异常   org.springframework.beans.factory.BeanCreationException:创建名为'userController'的bean时出错:注册自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.zzz.service.UserService com.zzz.controller.UserController.userService ;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[com.zzz.service.UserService]的限定bean依赖:期望至少有1个bean有资格作为autowire候选者依赖。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}       在       ......

的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" />

调度-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

有2个自动装配错误:

无法自动装配字段: private com.zzz.repository.UserRepository com.zzz.service.UserService.userRepository

无法自动装配字段: private com.zzz.service.UserService com.zzz.controller.UserController.userService

更新2

我尝试使用servlet-dispatcher.xml作为唯一的配置文件(将applicationContext.xml的内容放入其中),它工作正常。所以这是两个xml文件之间的问题,但我无法说出来。

更新3

我将applicationContext.xml重命名为root-context.xml,并在web.xml中将contextConfigLocation设置为它,错误消失了!

似乎是关于applicationContext.xml的一些配置错误,任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

另一个猜测:在调度程序xml中你只扫描控制器包...所以如果没有加载application.xml,它就不会扫描服务包,因此没有加载用户服务...会很有趣如果您在dispatcher.xml中扫描整个com.zzz包,会发生什么?