我正在尝试使用与org.springframework.cache.annotation.Cacheable类似的东西:
自定义注释:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CheckEntity {
String message() default "Check entity msg";
String key() default "";
}
方面:
@Component
@Aspect
public class CheckEntityAspect {
@Before("execution(* *.*(..)) && @annotation(checkEntity)")
public void checkEntity(JoinPoint joinPoint, CheckEntitty checkEntity) {
System.out.println("running entity check: " + joinPoint.getSignature().getName());
}
}
服务:
@Service
@Transactional
public class EntityServiceImpl implements EntityService {
@CheckEntity(key = "#id")
public Entity getEntity(Long id) {
return new Entity(id);
}
}
我的IDE(IntelliJ)没有看到与key =“#id”用法有什么特别之处,相比之下,Cacheable的类似用法与纯文本的颜色不同.我提到IDE部分只是作为一个提示,如果它有帮助,它看起来像IDE事先知道这些注释或它只是意识到我的例子中不存在的一些连接.
checkEntity.key中的值为“#id”而不是预期的数字.
我尝试使用ExpressionParser,但可能没有以正确的方式.
在checkEntity注释中获取参数值的唯一方法是访问arguments数组,这不是我想要的,因为这个注释也可以在具有多个参数的方法中使用.
任何的想法?