Tito
Tito

Reputation: 9044

Cannot find bean definition , should I annotate an interface too?

I have an interface as show below

   public interface UserManager {

      void insertUser(User user);
   .......

Now I have an implementation class as below

@Service
public class UserManagerImpl implements UserManager {

    @Autowired
    private UserDAO userDAO;

In my controller

@Controller
public class ExampleGizmoController {

    @Autowired
    private UserManager userManager;

UserDAOImpl is

@Service
public class UserDAOImpl implements UserDAO {

    @Autowired
    private SessionFactory sessionFactory;

My application-context.xml

<context:annotation-config/>
<context:component-scan base-package="com.foo" />

which scans all my packages.I have deployed it as war file and when the deployment happens, The userManager property is not getting autowired to the ExampleGizmoController and shows the error in tomcat as below

Spring-MVC threw load() exception: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.foo.UserManager] 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)}

I was able to make out that autowiring was not happening , even though it was annotation driven and component scanning is turned on. Is there anything else I should do for autowiring to work ?

Upvotes: 0

Views: 1227

Answers (4)

honorius03
honorius03

Reputation: 23

Same error i faced, but i've got one more dao class which retrieves information of user manager. You should add @Repository annotation to dao class. Your another dao class looks like that;

@Repository("userManagerDao")
public class UserManagerDAOImpl implements UserManagerDao{
    public UserManagerDTO createNewUserManager() {
        UserManagerDTO userManager = new UserManagerDTO();
        userManager.setId(1);
        userManager.setFirstName("First Name");
        userManager.setLastName("Last Name");
        return userManager;
    }
}

Upvotes: 0

cRx
cRx

Reputation: 39

You should use @Service("userManager"). Its a way of telling Spring that you want to name your UserManagerImpl bean instance with "userManager".

Upvotes: 0

x3mik
x3mik

Reputation: 61

Maybe it's stupid... but try to remove implements UserManager from UserManager Impl ..

Upvotes: 0

NimChimpsky
NimChimpsky

Reputation: 47280

 <mvc:annotation-driven/>

is also required in your config file

Upvotes: 1

Related Questions