Results 1 to 3 of 3

Thread: Injecting mongodb DAO : bean not found

  1. #1
    Join Date
    Feb 2013
    Posts
    4

    Default Injecting mongodb DAO : bean not found

    Hello,

    I'm a total beginner to spring and IoC, and i got some difficulties to setup a Spring webapp.

    I'm actually trying to implement a mongodb DAO, and inject it in a service layer trough annotation.
    My DAO bean is not found by spring.

    Here's some part of my code :


    IUserDao.java
    Code:
    public interface IUserDao {
           //A Simple Interface, nothing special
    }
    UserDaoMongo.java
    Code:
    @Repository("UserDao")
    public class UserDaoMongo implements IUserDao {
           //My own implementation of the DAO
    }
    IUserService.java
    Code:
    public interface IUserService {
    	//Here again, a simple interface, nothing special
    }
    UserServiceImpl.java
    Code:
    @Service("UserService")
    public class UserServiceImpl implements IUserService, UserDetailsService {
    	
    	@Autowired(required=true)
    	@Qualifier("UserDao")
    	private IUserDao userDao;
    
    	//This one is the funny part. My UserService implement UserDetailsService, to be used with spring security.
    	//I Thought it was the good place to that.
    }
    context-mongo.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:beans="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	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-3.2.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    		http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd">
    	
    	<mongo:mongo host="${mongo.address}" port="${mongo.port}" />
    	<beans:bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    		<beans:constructor-arg ref="mongo" />
    		<beans:constructor-arg name="databaseName" value="${mongo.database}" />
     	</beans:bean>
    
    	<context:annotation-config />
    </beans:beans>
    And i got the following error :
    Code:
     org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [my.package.dao.IUserDao] 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), @org.springframework.beans.factory.annotation.Qualifier(value=UserDao)}
    I already tried to add "<repositories base-package="" />" without success.
    I tryed to googling my problem, but all solutions found wasn't really helpful. Or may i wasn't able to totally understand my mistake and wasn't able to know what to search exactly.

    Also i don't know how to investigate further to understand this problem. I'm not only a beginer to Spring, but also to Java.

    Thanks for any help.

  2. #2
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    492

    Default

    You don't have component scanning enabled. Use <context:component-scan /> over <context:annotation-config />.

  3. #3
    Join Date
    Feb 2013
    Posts
    4

    Default

    Thanks for the tips, it work correctly now.

    context-mongo.xml is imported in servlet-context.xml.
    servlet-context.xml already call <context:component-scan /> with a correct base-package attribute. I thought it was enough. Obviously, it was not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •