Announcement Announcement Module
Collapse
No announcement yet.
beanOfTypeIncludingAncestors Doesn't honor @Primary Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • beanOfTypeIncludingAncestors Doesn't honor @Primary

    I have a Repository and a child which implements that interface and delegates to the original, so that it may spy on calls to the repository. I.e.
    Code:
     
    @Repository
    public interface ThingRepository {
    List<Thing> findByThingName(String name);
    }
    Code:
    @Primary
    @Component
    public class ThingDelegateRepository implements {
    
    @Autowired
    @Qualifier(name= "thingRepository")
    ThingRepository thingRepository;
    
    List<Thing> findByName(String name) {
    doImportWork(name);
    return thingRepository.findByName(name);
    }
    
    }
    They are both picked by by component scanning. The goal here is to always step between anyone who wants a repository and the raw repository, unless they specifically request the raw repository by name.

    If I put debug break points in the start up I can look inside the DefaultListenableBeanFactory and see that primary is set to true on the ThingDelegateRepository's bean definition. My problem comes at start-up, the class org.springframework.data.repository.support.Reposi tories constructor calls BeanUtils.beanOfTypeIncludingAncestors which calls beansOfTypeIncludingAncestors and then checks that there is only one. This whole system is not checking the isParmy flag like a call to DefaultListenableBeanFactory would. So it seems to me that @Primary cannot be used with repositories?

    Is this behaviour by design? Does anyone know if there is an alternate way of achieving the same thing?

    Thanks for any help!
Working...
X