我无法为spring数据jpa存储库提供建议。目的是void在特定存储库中用自定义注释(在此示例中为ResourceNotFound)进行注释的所有非公共方法(在周围),并在返回值为null或者为空集合时引发异常。
void
null
@Repository @ResourceNotFound @Transactional(readOnly = true) public interface CityRepository extends JpaRepository<City, Long>, JpaSpecificationExecutor<City> { … }
以下建议是将用注释的接口的实现的所有公共方法联系起来@ResourceNotFound。
@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为:
publicMethodInsideAClassMarkedWithResourceNotFound(…)
@Pointcut("execution(public * package.CityRepository+.*(..))")
但是,@ResourceNotFound未拾取注释。这可能是由于存储库接口的基础类是一个(代理的)SimpleJpaRepository不具有该特定注释的事实。
SimpleJpaRepository
有没有办法传播@ResourceNotFound到实现?
-更新-
更改了问题以反映以下事实:建议(仅)应仅适用于具有自定义注释的存储库。
尽管OP严重依赖于AspectJ解决方案,但目前的问题并不直接表明解决方案应限于AspectJ。因此,我想提供一种非AspectJ的方式来建议Spring Data JPA存储库。它基于将自定义添加Interceptor到准系统Spring AOP代理拦截器链中。
Interceptor
首先,配置你的自定义RepositoryFactoryBean,例如
RepositoryFactoryBean
@Configuration @EnableJpaRepositories(repositoryFactoryBeanClass = CustomRepositoryFactoryBean.class) public class ConfigJpaRepositories { }
接下来,实施CustomRepositoryFactoryBean添加自己RepositoryProxyPostProcessor的JpaRepositoryFactory
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实现应将你添加MethodInterceptor到ProxyFactory特定存储库(检查RepositoryInformation)中:
MethodIntercepto
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建议的全部功能:
MethodInterceptor
org.aopalliance.aop.Advice
@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; } }
如果要在存储库级别拦截存储库调用,则实际上不需要为此引入自定义注释。你应该能够使用普通类型匹配来实现此功能:
@Pointcut("execution(public !void org.springframework.data.repository.Repository+.*(..))")
这将拦截所有void扩展Spring Data Repository接口的Spring bean 的所有非方法的执行。
Spring Data Repository
Spring bean