简体   繁体   中英

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

My @Controller class and CustomerRepository are in same package.

I can't get the problem causing this exception.

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

EDIT: Complete conf.xml structure for resolving problem. Please switch to 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. You can do this with this line in your spring-config.xml:

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

more docs here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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