0

I am trying to create an instance of my custom repository post initialization. The goal is that I give the class (PineappleRepository.class) and it returns the object (PineappleRepository). Here the Custom Repository:

public interface FoodRepository<T extends FoodEntity> extends JpaRepository<T, UUID>, FoodRepositoryCustom {
}

public interface FoodRepositoryCustom {
    List<FoodEntity> executeQuery(Map<String, String> query);
}

@Repository("Repo")
public class FoodRepositoryCustomImpl implements FoodRepositoryCustom {


    private final EntityManager em;

    public FoodRepositoryCustomImpl(EntityManager em) {
        this.em = em;
    }

    @Override
    public List<FoodEntity> executeQuery(Map<String, String> query) {
        // Boring code
        return new List<>();
    }
}

Here is a wacky form of java sudo code, of what I am roughly looking for:

public <F extends FoodEntity> FoodRepository<F> getFoodRepository(Class<? extends FoodRepository<F>> clazz) {
    // Perform magical generation of FoodRepository<F>
    return foodRepository;
}

Any help is much appreciated, -AwesomeDude091

Edit: ApplicationContext or autowired will not work as the idea is that this repository is initalized post init. So outside of the Repo Scan, etc.

AwesomeDude091
  • 71
  • 1
  • 12
  • Did you try [`ApplicationContext#getBean`](https://stackoverflow.com/a/34089279/10871900)? – dan1st Oct 15 '22 at 13:39
  • The bean isn't registered, the Idea is that I register it post init – AwesomeDude091 Oct 15 '22 at 14:33
  • [`@PostConstruct`](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-postconstruct-and-predestroy-annotations)!? More general: ["initialization callbacks"](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-lifecycle-initializingbean) – xerx593 Oct 15 '22 at 14:40
  • You can call `ApplicationContext#getBean` after registration as well. – dan1st Oct 15 '22 at 14:44

0 Answers0