-
Notifications
You must be signed in to change notification settings - Fork 1.5k
@EnableJpaRepositories(repositoryBaseClass = ...) doesn't support dependency injection #3384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is by design as the base repository class is parametrized using the entity information and EntityManager. Taking a step back, what do you want to achieve that isn't achievable by using repository fragments? |
Unless my understanding of repository fragments is incorrect (which is entirely possible), fragments seem to have a few requirements that didn't meet my needs:
So, I went the extending the base class route. Also, the main problem regarding the dependency injection I raised was due to the fact that the functionality I'm adding requires a per repository @ConfigurationProperties bean. Basically, I'm trying to do something similar to the @EnableJpaRepositories functionality for multiple repositories. DataSource1 -> Repo1 Spring supports that scenario out of the box, but I'm going for: DataSource1 + ConfigurationProperties1 -> Repo1 I'm trying to follow the pattern that Spring lays out with auto injecting the data source, but since I can't enhance the annotation, I'm following the other Spring pattern of using a naming convention. So, if the entity for the repo is called EntityOne, for example, I expect a configuration properties bean named entityOneRepoProperties. To do this today, I have to have a custom factory and factory bean that @autowire the ApplicationContext and pass in the configuration properties through modifying this part:
If I could have simply injected an ApplicationContext into the repo impl, I could have done that much easier. |
I'm trying fix it by spring-projects/spring-data-commons#3018.
It's not true, but must be in |
Yeah, that's what I meant. That doesn't make sense if you want to share the fragment across multiple |
I agree, the PR will address it, would you take some time to verify? |
I think that fix will make it easier for sharing fragments, but in my case, I don't think it will work since I need to get a bean in the implementation so had to use a full blown implementation and custom Is there a way to get a bean into the fragment? |
I think you want something with this declined PR. But if spring-projects/spring-data-commons#3018 is accepted, you can do like this: public interface BeanProvider {
<T> T getBean(Class<T> clazz);
}
@RequiredArgsConstructor
public class BeanProviderImpl implements BeanProvider {
private final BeanFactory beanFactory;
@Override
public <T> T getBean(Class<T> clazz) {
return this.beanFactory.getBean(clazz);
}
}
interface TestEntityRepository extends CrudRepository<TestEntity, Long>, BeanProvider {
default void saveByEntityManager(TestEntity entity) {
getBean(EntityManager.class).persist(entity);
}
default void saveBySelf(TestEntity entity) {
getBean(TestEntityRepository.class).save(entity);
}
} |
You can take a more invasive approach by subclassing Finally, you need to configure |
Yeah, that's what I ended up doing, but I didn't make it autowired, I just added it to the Its definitely "invasive" :). What @quaff proposed above should make things a lot easier. |
We do not want to introduce more complexity than we already have in that area. This isn't a common request and having that extra mile on your side is fine for us for the time being. |
I have a configuration class that specifies a custom repo implementation:
This forces a constructor signature of:
And does not support @Autowired or any method I could find to pass in beans.
Can this be modified to instantiate the class via IOC to support pulling in beans?
The text was updated successfully, but these errors were encountered: