0
我有一系列扩展基本服务的服务。在这个基本服务中,我实例化一个类,用于轮询数据库并根据其内容发送通知,这个轮询的时间由Spring处理。我期望的是,对于扩展基本服务的每个服务应该有一个此轮询器的实例,但根据我放置@Scheduled注释的位置,它不起作用。当@Scheduled注释位于轮询类内时,Spring任务执行程序只创建一个实例
我想是这样的:
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
}
}
它按预期工作,但不符合设计目标。
有没有办法让计划的注释留在轮询中,同时确保每个扩展服务都得到它自己的实例?