Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
spring引导方面是否在调度方法内的方法上工作_Spring_Aspect_Spring Scheduled - Fatal编程技术网

spring引导方面是否在调度方法内的方法上工作

spring引导方面是否在调度方法内的方法上工作,spring,aspect,spring-scheduled,Spring,Aspect,Spring Scheduled,用于spring引导应用程序 我让我的方面在我的计划方法中监听我的私有或公共方法 但它不起作用。但是,方面可以监听我的预定方法 下面是我的github上的一个示例 有人知道答案或原因吗?或者如何解决呢 感谢由于无法代理,Aspects无法在同一类中调用其他方法 这意味着自调用不会导致与方法调用关联的通知有机会执行 好吧,那我们该怎么办?最好的方法(这里宽松地使用术语best)是重构代码,这样就不会发生自调用 关于代理私有方法的注意事项: 由于Spring的AOP框架基于代理的特性,受保护的方法

用于spring引导应用程序

我让我的方面在我的计划方法中监听我的私有或公共方法

但它不起作用。但是,方面可以监听我的预定方法

下面是我的github上的一个示例

有人知道答案或原因吗?或者如何解决呢


感谢

由于无法代理,Aspects无法在同一类中调用其他方法

这意味着自调用不会导致与方法调用关联的通知有机会执行

好吧,那我们该怎么办?最好的方法(这里宽松地使用术语best)是重构代码,这样就不会发生自调用

关于代理私有方法的注意事项:

由于Spring的AOP框架基于代理的特性,受保护的方法根据定义是不被拦截的,既不适用于JDK代理(如果这不适用),也不适用于CGLIB代理(如果这在技术上是可能的,但不推荐用于AOP目的)。因此,任何给定的切入点将只与公共方法匹配

如果您的侦听需要包括受保护的/私有的方法,甚至是构造函数,请考虑使用Spring驱动的原生AspectJ编织,而不是Spring基于代理的AOP框架。这构成了一种不同的AOP使用模式,具有不同的特性,因此在做出决定之前,一定要先熟悉编织

参考:

更改代码如下:

@Component
public  class Test{

    public void reportInPrivateMethod() {
        System.out.println("private method");
       
    }

    public void reportInPublicMethod() {
        System.out.println("public method");
       
    }
}
现在调用此方法:

@Component
public class ScheduledTasks {

    @Autowired
    private Test test;

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        test.reportInPrivateMethod();
        test.reportInPublicMethod();
        log.info("The time is now {}", dateFormat.format(new Date()));
    }


}
根据更改修改方面:

@Aspect
@Component
public class Monitor {

   @AfterReturning("execution(* com.zeph.aop.ScheduledTasks.reportCurrentTime())")
public void logServiceAccess(JoinPoint joinPoint) {
    System.out.println("Completed: " + joinPoint);
}

@AfterReturning("execution(* com.zeph.aop.Test.reportInPrivateMethod())")
public void logServiceAccessPrivateMethod() {
    System.out.println("Completed  PRIVATE :");
}

@AfterReturning("execution(* com.zeph.aop.Test.reportInPublicMethod())")
public void logServiceAccessPublicMethod() {
    System.out.println("Completed PUBLIC: ");
}
}

Aspects不能在同一类中调用其他方法,因为它不能被代理

这意味着自调用不会导致与方法调用关联的通知有机会执行

好吧,那我们该怎么办?最好的方法(这里宽松地使用术语best)是重构代码,这样就不会发生自调用

关于代理私有方法的注意事项:

由于Spring的AOP框架基于代理的特性,受保护的方法根据定义是不被拦截的,既不适用于JDK代理(如果这不适用),也不适用于CGLIB代理(如果这在技术上是可能的,但不推荐用于AOP目的)。因此,任何给定的切入点将只与公共方法匹配

如果您的侦听需要包括受保护的/私有的方法,甚至是构造函数,请考虑使用Spring驱动的原生AspectJ编织,而不是Spring基于代理的AOP框架。这构成了一种不同的AOP使用模式,具有不同的特性,因此在做出决定之前,一定要先熟悉编织

参考:

更改代码如下:

@Component
public  class Test{

    public void reportInPrivateMethod() {
        System.out.println("private method");
       
    }

    public void reportInPublicMethod() {
        System.out.println("public method");
       
    }
}
现在调用此方法:

@Component
public class ScheduledTasks {

    @Autowired
    private Test test;

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        test.reportInPrivateMethod();
        test.reportInPublicMethod();
        log.info("The time is now {}", dateFormat.format(new Date()));
    }


}
根据更改修改方面:

@Aspect
@Component
public class Monitor {

   @AfterReturning("execution(* com.zeph.aop.ScheduledTasks.reportCurrentTime())")
public void logServiceAccess(JoinPoint joinPoint) {
    System.out.println("Completed: " + joinPoint);
}

@AfterReturning("execution(* com.zeph.aop.Test.reportInPrivateMethod())")
public void logServiceAccessPrivateMethod() {
    System.out.println("Completed  PRIVATE :");
}

@AfterReturning("execution(* com.zeph.aop.Test.reportInPublicMethod())")
public void logServiceAccessPublicMethod() {
    System.out.println("Completed PUBLIC: ");
}
}

谢谢你的解释。谢谢你的解释。