我有一系列扩展基础服务的服务。在这个基础服务中,我实例化了一个用于轮询数据库并根据其内容发送通知的类,该轮询的时间由 spring 处理。我期望的是每个扩展基本服务的服务都应该有一个这个轮询器的实例,但是根据我放置@Scheduled 注释的位置,它不起作用。
我想要的是这样的:
public class Base {
    private Poller p = new Poller(this);
    // the rest of the service code
}
public class Poller{
    Base b;
    public Poller(Base B){
        b=B;
    }
    @Scheduled(fixedDelay=5000)
    public void poll(){
        //do stuff
        System.out.println(b.name); //doesn't work, causes really unhelpful errors
        System.out.println("----"); //prints as expected, but only once
                                    //regardless of how many extending services exist
    }
}
但它似乎只在所有扩展程序之间实例化一个轮询器。如果我像这样构造它:
public class Base {
    private Poller p = new Poller(this);
    // the rest of the service code
    @Scheduled(fixedDelay=5000)
    public void poll(){
        p.poll();
    }
}
public class Poller{
    Base b;
    public Poller(Base B){
        b=B;
    }
    public void poll(){
        //do stuff
        System.out.println(b.name); //prints the name of the service for each extender
        System.out.println("----"); //prints as expected, once for each extender
    }
}
它按预期工作,但不符合这里的设计目标。
有没有办法让预定的注释留在轮询器中,同时确保每个扩展服务都有自己的实例?