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

前端 未结 5 632
南旧
南旧 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:30

    What about @Primary?

    Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value. This annotation is semantically equivalent to the element's primary attribute in Spring XML.

    @Primary
    public class HibernateDeviceDao implements DeviceDao
    

    Or if you want your Jdbc version to be used by default:

    
    

    @Primary is also great for integration testing when you can easily replace production bean with stubbed version by annotating it.

提交回复
热议问题