Autowiring two beans implementing same interface - how to set default bean to autowire?

前端 未结 5 638
南旧
南旧 2020-11-28 17:50

Background:

I have a Spring 2.5/Java/Tomcat application. There is the following bean, which is used throughout the application in many places

publi         


        
5条回答
  •  庸人自扰
    2020-11-28 18:06

    I'd suggest marking the Hibernate DAO class with @Primary, i.e. (assuming you used @Repository on HibernateDeviceDao):

    @Primary
    @Repository
    public class HibernateDeviceDao implements DeviceDao
    

    This way it will be selected as the default autowire candididate, with no need to autowire-candidate on the other bean.

    Also, rather than using @Autowired @Qualifier, I find it more elegant to use @Resource for picking specific beans, i.e.

    @Resource(name="jdbcDeviceDao")
    DeviceDao deviceDao;
    

提交回复
热议问题