使用反射按类类型查找对象以实例化Spring数据存储库
我正试图对我的数据库应用程序进行“按类型查找”研究。使用反射按类类型查找对象以实例化Spring数据存储库,spring,spring-data,Spring,Spring Data,我正试图对我的数据库应用程序进行“按类型查找”研究。 特别是,我有一个类型的类层次结构:Mother我用getBean替换了createBean,它工作了。我用getBean替换了createBean,它工作了。您可能想看看Spring Data Commons中的helper类。您可以从一个ListableBeanFactory创建一个,然后通过托管域类型访问存储库。您可能想看看Spring Data Commons中的helper类。您可以从ListableBeanFactory创建一个,然
特别是,我有一个类型的类层次结构:Mother我用getBean替换了createBean,它工作了。我用getBean替换了createBean,它工作了。您可能想看看Spring Data Commons中的helper类。您可以从一个
ListableBeanFactory
创建一个,然后通过托管域类型访问存储库。您可能想看看Spring Data Commons中的helper类。您可以从ListableBeanFactory
创建一个,然后按托管域类型访问存储库
public List<? extends Mother> findMotherByType(Class classToSearch)
throws FindException {
PagingAndSortingRepository<? extends Mother, Long> repo;
try {
repo = beanFactory.createBean(classToSearch);
} catch (Exception e) {
throw new FindException (this
.getClass().getName(), classRepository);
}
return repo.findAll();
}
@Controller
public class ControllerClassForTheSearch implements BeanFactoryAware {
private AutowireCapableBeanFactory beanFactory;
@Override
public void setBeanFactory(final BeanFactory beanFactory)
throws BeansException {
this.beanFactory = (AutowireCapableBeanFactory) beanFactory;
}
[...] // rest of the class code