简体   繁体   English

Spring MVC MongoDB集成

[英]Spring MVC MongoDB integration

I would like to create Spring MVC sample with mongoDB by this tutorial http://spring.io/guides/gs/accessing-data-mongodb/ . 我想通过本教程http://spring.io/guides/gs/accessing-data-mongodb/使用mongoDB创建Spring MVC示例。 I try @Autowired for CustomerRepository repository into my @Controller class, but I receive org.springframework.beans.factory.BeanCreationException for repository class member with exception message "....No matching bean of type [com.mvc.venko.CustomerRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency..." 我尝试将@Autowired for CustomerRepository repository放入我的@Controller类中,但是收到存储库类成员的org.springframework.beans.factory.BeanCreationException ,出现异常消息“ ....没有类型为[com.mvc.venko.CustomerRepository的匹配豆” ]找到依赖项:至少应有1个bean符合此依赖项的自动装配条件...”

My @Controller class and CustomerRepository are in same package. 我的@Controller类和CustomerRepository在同一包中。

I can't get the problem causing this exception. 我无法解决导致此异常的问题。

PS: using springFramework 3.1.1.RELEASE, spring-data-mongodb 1.4.2.RELEASE PS:使用springFramework 3.1.1.RELEASE,spring-data-mongodb 1.4.2.RELEASE

EDIT: Complete conf.xml structure for resolving problem. 编辑:用于解决问题的完整conf.xml结构。 Please switch to Spring 3.2.5 请切换到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>

I think you don't configure a bean of CustomerRepository type. 我认为您没有配置CustomerRepository类型的bean。 You can do this with this line in your spring-config.xml: 您可以在spring-config.xml中使用以下行执行此操作:

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

more docs here 这里有更多文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM