如何检测/建议Spring Data(JPA)存储库?
我无法为spring数据jpa存储库提供建议。目的是void
在特定存储库中用自定义注释(在此示例中为ResourceNotFound)进行注释的所有非公共方法(在周围),并在返回值为null
或者为空集合时引发异常。
@Repository @ResourceNotFound
@Transactional(readOnly = true)
public interface CityRepository extends JpaRepository<City, Long>, JpaSpecificationExecutor<City> { … }
以下建议是将用注释的接口的实现的所有公共方法联系起来@ResourceNotFound
。
@Pointcut("within(com.digitalmisfits.spring.aop.annotation.ResourceNotFound *)")public void beanAnnotatedWithResourceNotFound() {}
@Pointcut("execution(public * *(..))")
public void publicMethod() {}
@Around("beanAnnotatedWithResourceNotFound() && publicMethod()")
public Object publicMethodInsideAClassMarkedWithResourceNotFound(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("publicMethodInsideAClassMarkedWithResourceNotFound " + pjp.getTarget().toString());;
Object retVal = pjp.proceed();
if(((MethodSignature) pjp.getSignature()).getReturnType() != Void.TYPE && isObjectEmpty(retVal))
throw new RuntimeException("isObjectEmpty == true");
return retVal;
}
该publicMethodInsideAClassMarkedWithResourceNotFound(…)
方法的工作原理,当切入点isspecified为:
@Pointcut("execution(public * package.CityRepository+.*(..))")
但是,@ResourceNotFound
未拾取注释。这可能是由于存储库接口的基础类是一个(代理的)SimpleJpaRepository
不具有该特定注释的事实。
有没有办法传播@ResourceNotFound到实现?
-更新-
更改了问题以反映以下事实:建议(仅)应仅适用于具有自定义注释的存储库。
回答:
尽管OP严重依赖于AspectJ解决方案,但目前的问题并不直接表明解决方案应限于AspectJ。因此,我想提供一种非AspectJ的方式来建议Spring Data JPA存储库。它基于将自定义添加Interceptor
到准系统Spring AOP代理拦截器链中。
首先,配置你的自定义RepositoryFactoryBean
,例如
@Configuration@EnableJpaRepositories(repositoryFactoryBeanClass = CustomRepositoryFactoryBean.class)
public class ConfigJpaRepositories {
}
接下来,实施CustomRepositoryFactoryBean
添加自己RepositoryProxyPostProcessor
的JpaRepositoryFactory
class CustomRepositoryFactoryBean<R extends JpaRepository<T, I>, T , I extends Serializable> extends JpaRepositoryFactoryBean<R, T, I> { protected RepositoryFactorySupport createRepositoryFactory(EntityManager em) {
RepositoryFactorySupport factory = super.createRepositoryFactory(em);
factory.addRepositoryProxyPostProcessor(new ResourceNotFoundProxyPostProcessor());
return factory;
}
}
你的RepositoryProxyPostProcessor
实现应将你添加MethodIntercepto
r到ProxyFactory
特定存储库(检查RepositoryInformation
)中:
class ResourceNotFoundProxyPostProcessor implements RepositoryProxyPostProcessor { @Override
public void postProcess(ProxyFactory factory, RepositoryInformation repositoryInformation) {
if (repositoryInformation.getRepositoryInterface().equals(CityRepository.class))
factory.addAdvice(new ResourceNotFoundMethodInterceptor());
}
}
并且在你MethodInterceptor
(BTW是的子接口org.aopalliance.aop.Advice
,所以仍然是一个建议:))中,你具有AspectJ @Around
建议的全部功能:
class ResourceNotFoundMethodInterceptor implements MethodInterceptor { @Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
ResourceNotFound resourceNotFound = method.getAnnotation(ResourceNotFound.class);
//...
Object result = invocation.proceed();
//...
return result;
}
}
以上是 如何检测/建议Spring Data(JPA)存储库? 的全部内容, 来源链接: utcz.com/qa/407211.html