有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring MVC MongoDB集成

我想通过本教程http://spring.io/guides/gs/accessing-data-mongodb/用mongoDB创建Spring MVC示例。我尝试将@Autowired for CustomerRepository repository放入我的@Controller类,但我收到了org.springframework.beans.factory.BeanCreationExceptionfor repository类成员,并显示异常消息“……没有为依赖项找到[com.mvc.venko.CustomerRepository]类型的匹配bean:预期至少有一个bean符合此依赖项的Autowired候选项……”

我的@Controller类和CustomerRepository在同一个包中

我无法理解导致此异常的问题

PS:使用springFramework 3.1.1。spring data mongodb 1.4.2发布。释放

编辑: 用于解决问题的完整conf.xml结构。请切换到Spring 3.2.5

<?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:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->
    <mongo:repositories base-package="com.mvc.venko" />
    <mongo:mongo host="127.0.0.1" port="27017" />
    <mongo:db-factory dbname="yourdb" write-concern="NONE" />
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>
</beans>

共 (1) 个答案

  1. # 1 楼答案

    我认为您没有配置CustomerRepository类型的bean。 您可以在spring配置中使用这一行来实现这一点。xml:

    <mongo:repositories base-package="/path to your repository package/" />
    

    更多文档here