Spring Aop:从未达成建议

时间:2018-11-12 15:30:49

标签: spring spring-boot spring-aop

我已经创建了这个切入点:

@Pointcut(value = "execution(* net.space.service.RepositoryService.createDocumentFromBytes(..))")
public void groupBytesMethod() {
}

和此建议:

@Around("groupBytesMethod()")
public Object groupMetrics(ProceedingJoinPoint point) throws Throwable {
    Object result = point.proceed();
}

我已经设置了一个断点,但是从未达到。

package net.space.service;

@Service
public class RepositoryService {
    private Reference createDocumentFromBytes(String id, byte[] content) throws IOException {...}
    public Reference groupDocuments(@NotNull RepositoryGroupForm groupForm) {
        return this.createDocumentFromBytes("id", new byte[10]);
    }
}

1 个答案:

答案 0 :(得分:0)

有很多方法可以使这项工作,我建议使用注释,例如:

@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface GroupBytesMethod {
}

然后

@Aspect
@Component
public class MyAspect {

    @Around("@annotation(GroupBytesMethod)") // use full package name of annotation
    public Object groupMetrics(ProceedingJoinPoint point) throws Throwable {
        // do something before
        Object result = null;
        try {
          result = point.proceed();
        }
        catch (Throwable t) {
          // handle an exception
        }
        // do something after
        return result;
    }
}

示例here