I'm just starting out with Spring Data and I'm trying to add a custom method to my repositories which requires another bean (which is preferably only created once (i.e. singleton))
The bean is declared in the root-context.xml like so
<bean class="org...CachedQueryTemplateFactory" />
With the proper namespace of course. I then try to inject this bean into a CustomRepositoryImpl using @Autowired
@Getter
@Setter
@Component
public class StudyRepositoryImpl implements StudyRepositoryCustom {
@PersistenceContext private EntityManager d_em;
@Autowired private QueryTemplateFactory queryTemplateFactory;
@Override
public List<Study> findStudies(
UUID indication,
List<UUID> variables,
List<UUID> treatments) {
QueryTemplate template = this.queryTemplateFactory.buildQueryTemplate("...");
...
}
}
However when running the code I get NullPointerException. When doing the wiring in a @Controller and then passing the reference to the repository it works, but I don't want to DI to happen in the controller. So why is the QueryTemplateFactory null in the StudyRepositoryImpl but not for the @Controller and how can I fix this?
Full code is available on GitHub https://github.com/joelkuiper/trialverse/tree/feature/injectQueryTemplate
Thanks in advance!