我有一个简单的webapp,它使用很少的spring组件 - Spring Boot , MVC 和 Spring Data 来进行持久性管理(所有内容都是用使用 Gradle 构建 Groovy 。 我正在尝试为所有存储库添加一些通用逻辑,如documentation中所述。我创建了以下工件:
GenericObjectRepository
这是一个定义我的常用方法的接口:
@NoRepositoryBean
interface GenericObjectRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
String sharedCustomMethod(String name)
}
GenericObjectRepositoryImpl
实现以上界面:
class GenericObjectRepositoryImpl<T, ID extends Serializable>
extends SimpleJpaRepository<T, ID> implements GenericObjectRepository<T, ID> {
EntityManager entityManager
GenericObjectRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {
super(domainClass, entityManager)
this.entityManager = entityManager;
}
@Override
String sharedCustomMethod(String name) {
"Hello ${name}"
}
}
最后是存储库(使用一些随机数据特定的方法)
@Repository
interface MarketingClientRepository extends GenericObjectRepository<MarketingClient, Long> {
List<MarketingClient> findByLastName(String lastName);
}
问题在于,每次启动应用程序时,Spring都会尝试即时MarketingClientRepository
,我会收到以下异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'marketingClientRepository': Invocation of init method failed;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
...
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property shared found for type MarketingClient!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:359)
...
在我看来,Spring Data正在尝试解析我的方法(sharedCustomMethod
),提取shared
部分并尝试将其所做的所有魔法应用于其他所有Repository接口方法(尝试访问我的业务对象上的shared
属性 - 显然不存在。)
提前致谢,
编辑1:
我在步骤3中添加了FactoryBean,但我没有编译它。我不知道泛型是否有问题(我承认这种结构对我来说非常复杂)。类代码如下:
class GenericRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable>
extends JpaRepositoryFactoryBean<R, T, I> {
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
new GenericRepositoryFactoryBean(entityManager)
}
private static class GenericRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {
private EntityManager entityManager
GenericRepositoryFactory(EntityManager entityManager) {
super(entityManager)
this.entityManager = entityManager
}
protected Object getTargetRepository(RepositoryMetadata metadata) {
new GenericObjectRepositoryImpl<T, I>((Class<T>) metadata.getDomainType(), entityManager)
}
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
GenericObjectRepository.class
}
}
}
编译器说:
/.../GenericRepositoryFactoryBean.groovy: 14: unable to resolve class T
@ line 14, column 60.
yBean<R extends JpaRepository<T, I>, T,
^
/.../GenericRepositoryFactoryBean.groovy: 14: unable to resolve class I
@ line 14, column 63.
an<R extends JpaRepository<T, I>, T, I e
^
2 errors
答案 0 :(得分:1)
您基本上错过了reference documentation中描述的完成此项工作所需的步骤中的第3步。
由于这个过程相当尴尬,我已经提交ticket来简化此过程。