
有两个实现类同时实现同一个接口
解决方案:
- @Component
- public class personA implements BeanPerson {
- //
- }
-
- @Component
- public class personB implements BeanPerson {
- //
- }
-
- @Component
- public class TestBean {
- @Autowired
- @Qualifier("personA")
- private BeanPerson beanPerson;
- }
@Primary的注解,优先注入加了 Primary 注解的 Bean ,不会抛异常 - @Component
- @Primary
- public class personA implements BeanPerson {
- //
- }
- @Component
- public class BeanPerson implements InitializingBean {
- @Autowired
- private ApplicationContext context;
- @Override
- public void afterPropertiesSet() {
- context.getBean("xxxBean");
- }
- }
- Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
- No bean named 'xxxBean' is defined

