1

在 Strtus2 Action 类中,我有名称如 getData()、getDetails() 的方法。除了这些,我的动作课上还有几个 getter/setter。我想使用 Spring 的 AOP 功能进行日志记录。我需要为 getData() 和 getDetails() 方法维护 Log,这反过来又调用服务类,但我不想为 action 类中存在的 getter/setter 维护 Log。
我不想在我的方面硬编码方法名称。不知道如何实现这一点。请帮帮我。

我的 AOP Logger 类是这样的:

@Before("myPointCut() && allService()")
public void logEntryService(JoinPoint joinPoint) {
    System.out.println("Start of " + joinPoint.getSignature().getName() + "() of "+joinPoint.getThis());
    start = System.currentTimeMillis();
}


@After("myPointCut() && allService()")
public void logExitService(JoinPoint joinPoint) {
    System.out.println("End of " + joinPoint.getSignature().getName() + "() of "+joinPoint.getThis());
    start = System.currentTimeMillis();
}

@Pointcut("@annotation(com.ing.trialbal.log.LogThis)")
public void myPointCut() {
    System.out.println("*************My PointCut");
}

@Pointcut("execution(* com.ing.trialbal.*.*.*(..))")
public void allService() {
    System.out.println("************All service");
}

LogThis.java

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogThis {

}

但这在我的应用程序中创建了非常紧密的耦合,因为我总是必须在服务中的每个方法和 dao 上编写 @Logthis 以获得它们的日志。

4

1 回答 1

1

与 绑定的切入点@annotation()。就像是

注释类

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Loggable {}

AOP 记录器

@Pointcut("@annotation(com.Loggable)") //  && execution( ... )

动作类

@Loggable
Object getData() {}

@Loggable
Object getDetails() {}
于 2013-07-03T07:00:30.207 回答