Spring aop: совет не был достигнут - PullRequest
0 голосов
/ 12 ноября 2018

Я создал этот pointcut:

@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 голосов
/ 12 ноября 2018

Есть много способов сделать эту работу, я бы предложил использовать аннотацию, что-то вроде:

@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;
    }
}

Пример здесь

...