Aspect仅适用于Aspect类所在的包上的类

时间:2019-06-24 09:07:23

标签: java spring spring-boot aop spring-aop

我正在尝试使用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);
    }
}

0 个答案:

没有答案