試試這個。也許你必須排除你的方面類以避免無限循環。這個例子捕捉所有方法與com..login .. *封裝
@Aspect
@SuppressAjWarnings({ "adviceDidNotMatch" })
public class AllMethodsAspect {
private static Map<String, Long> beforeTimestamps = new HashMap<>();
@Pointcut("!within(aspects..*)"
+ " && (!execution(* org..* (..)) && !within(org..*) && !call(* org..* (..)))"
+ " && (!execution(* java..* (..)) && !within(java..*) && !call(* java..* (..)))"
+ " && (!execution(* javax..* (..)) && !within(javax..*) && !call(* javax..* (..)))"
+ " && (!execution(* sun..* (..)) && !within(sun..*) && !call(* sun..* (..)))"
+ " && execution(* com..login..*(..))")
public void methodCall() {
}
@Before("methodCall()")
public void before(JoinPoint joinPoint) {
beforeMethodCall(joinPoint);
}
@AfterReturning(pointcut = "methodCall()", returning = "returnObject")
public void after(JoinPoint joinPoint, Object returnObject) {
afterMethodCall(joinPoint, returnObject);
}
@AfterThrowing(pointcut = "methodCall()", throwing = "throwable")
public void throwing(JoinPoint joinPoint, Throwable throwable) {
afterThrowingMethodCall(joinPoint, throwable);
}
void beforeMethodCall(JoinPoint joinPoint) {
try {
long start = System.currentTimeMillis();
beforeTimestamps.put(joinPoint.toString() + " - " + Thread.currentThread().getName(), Long.valueOf(start));
LOG.info(".before " + joinPoint);
} catch (Exception e) {
LOG.error(".before Exception " + e);
}
}
void afterMethodCall(JoinPoint joinPoint, Object returnObject) {
afterMethodCall(joinPoint, returnObject, 0);
}
void afterMethodCall(JoinPoint joinPoint, Object returnObject, int depth) {
try {
long start = beforeTimestamps.get(joinPoint.toString() + " - " + Thread.currentThread().getName()).longValue();
beforeTimestamps.remove(joinPoint.toString() + " - " + Thread.currentThread().getName());
long duration = System.currentTimeMillis() - start;
Signature signature = joinPoint.getSignature();
if (signature instanceof MethodSignature) {
Class<?> returnType = ((MethodSignature) signature).getReturnType();
LOG.info(".after " + joinPoint + " " + duration + "ms" + (void.class == returnType ? "" : " [" + returnObject + "]"));
} else if (signature instanceof ConstructorSignature) {
LOG.info(".after " + joinPoint + " " + duration + "ms Constructor");
} else if (signature instanceof FieldSignature) {
LOG.info(".after " + joinPoint + " " + duration + "ms Field");
} else {
LOG.info(".after " + joinPoint + " " + duration + "ms unknown");
}
} catch (Exception e) {
LOG.error(".after Exception " + e);
}
}
void afterThrowingMethodCall(JoinPoint joinPoint, Throwable throwable) {
try {
Long startAsLong = beforeTimestamps.get(joinPoint.toString() + " - " + Thread.currentThread().getName());
long start = startAsLong == null ? 0 : startAsLong.longValue();
beforeTimestamps.remove(joinPoint.toString() + " - " + Thread.currentThread().getName());
long duration = System.currentTimeMillis() - start;
LOG.info(".fail " + joinPoint.toString() + " " + duration + " ms - " + throwable.getMessage());
} catch (NullPointerException e) {
LOG.info(".fail NullPointerException " + "unknown - " + throwable.getMessage());
}
}
static final class LOG {
static void info(String loggingData) {
System.err.println(new Date() + " " + loggingData);
}
static void error(String loggingData) {
System.err.println(new Date() + " " + loggingData);
}
}
}
如果它匹配包文件夾_named_'service'或包文件夾_containing_'service'太(像'com.test.fooservice1.Type')? –
@NándorElődFekete我想使它成爲一個庫,以便它可以掃描客戶端的所有軟件包並攔截包含關鍵字「service」的客戶端軟件包 – Prathiba530
@NándorElődFekete關鍵字「service」可以在像com .domain.test。service.Type(..)'和'com.test.service.impl.HelloWorld(..)' – Prathiba530