首页 > 解决方案 > Aspect 仅适用于 Aspect 类所在包上的类

问题描述

我正在尝试使用 AOP 记录方法的进入/退出。但是我只能记录作为 Aspect 类所在包的一部分的方法的进入/退出。我无法记录其他包中的方法.请帮助。提前致谢。

这是我的代码:

@Component
@Aspect
public class MyAspect {

    @Before("execution(* com.dave.school.student..*(..))")
    public void before(JoinPoint joinPoint)
    {
        System.out.println("Before::");
        System.out.println(joinPoint.getSignature().getDeclaringTypeName() + "/" + joinPoint.getSignature().getName());
        System.out.println(Arrays.toString(joinPoint.getArgs()));
    }


    @Before("execution(* com.dave.school.teachers..*(..))")
    public void before1(JoinPoint joinPoint) //doesn't work
    {
        System.out.println("Before::");
        System.out.println(joinPoint.getSignature().getDeclaringTypeName() + "/" + joinPoint.getSignature().getName());
        System.out.println(Arrays.toString(joinPoint.getArgs()));
    }

    @AfterReturning(pointcut ="execution(* com.dave.school.student..*(..))",returning = "result")
    public void after(JoinPoint joinPoint,Object result) throws JsonProcessingException {
        System.out.println("After::");
        System.out.println(joinPoint.getSignature().getDeclaringType().getName());
        ObjectMapper objectMapper=new ObjectMapper();
        String r=objectMapper.writeValueAsString(result);
        System.out.println("Result is ::" + r );
    }
}

这是我的春季启动应用程序:

@SpringBootApplication(scanBasePackages = {"com.dave.school"})
@EnableJpaRepositories(basePackages = {"com.dave.school"})
@EntityScan(basePackages = {"com.dave.school"})
@ComponentScan(basePackages = {"com.dave.school"})
public class Application
{
    public static void main(String[] args)
    {

        SpringApplication.run(Application.class, args);
    }
}

标签: javaspringspring-bootaopspring-aop

解决方案


推荐阅读