8

(spring mvc)first i do not know whether the writing below are right or not.if it is right ,then i don't understand how the @autowired works here.if it is wrong , then how i should do when i have more than one classes to implement one interface.

public interface UserDao{
    public User findUserByUserName(String username);
}

public class UserDaoImpl1 implements UserDao{

    @Override
    public User findUserByUserName(String username){
            .......
    }
}

public class UserDaoImpl2 implements UserDao{
    @Override
    public User findUserByUserName(String username){
            .......
    }
}

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserDao userDao;//how does atuowired work here?

    @Override
    public User loginCheck(User user){
                ......
        }
}
lhao0301
  • 1,941
  • 4
  • 20
  • 26

1 Answers1

8

When you have more than one class you can do two things:

  1. Use @Qualifierannotation and tell which implementation should be injected (spring default qualifier is name of bean) so doing this will inject second bean implementation:

    @Autowired
    @Qualifier("userDaoImpl2")
    private UserDao userDao;
    
  2. You can use @Primary on beans so that one implementation would be always preferred over another when there are more than one and interface is @Autowire.

Choice can be made based on side which should know about autowiring, if you want classes which are injected with dependencies to be ease to change and unaware of implementation details you should go with option 2 and if you want to control dependencies option 1 is better choice.

If more than one option exists Spring should throw exception (so your code should throw exception telling you more than one candidate for autowiring exists). It should look like:

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.package.name.UserDao] is defined: expected single matching bean but found 2: [userDaoImpl1, userDaoImpl2]

Here is good link that explains details.

Nenad Bozic
  • 3,724
  • 19
  • 45
  • This did not answer your question, do you need something more to be clarified? – Nenad Bozic Mar 23 '15 at 15:01
  • Thank you for your kindness.it have helped me a lot.I have studied Spring or programming for not a long time and I feel I have no need to consider too much before I am familiar with it.Thank you again. – lhao0301 Mar 24 '15 at 15:51
  • I am asking this because you first accepted and than unaccepted answer so I figured something is missing. Please accept answer if it solved your problem, others will also see it as answered which is how SO works. – Nenad Bozic Mar 24 '15 at 17:30
  • I am sorry for it.I will remember it. – lhao0301 Mar 25 '15 at 01:00
  • if use this- then it will be tightly coupled right ????- we dont want to go with tighly coupled @Autowired @Qualifier("userDaoImpl2") private UserDao userDao – Ashish Agrawal Yodlee Apr 16 '19 at 15:15