gpt4 book ai didi

java - 异常: org. springframework.beans.factory.BeanCurrentlyInCreationException

转载 作者:行者123 更新时间:2023-12-02 01:08:35 27 4
gpt4 key购买 nike

我想在抽象类上有计划注释,以便子类不需要有注释。抽象类中有类似的东西

@EnableScheduling
@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

public abstract class AbstractTestClass {

@Bean
public String getDelay() {
return "1000";
}

@Scheduled(fixedRateString = "#{@getDelay}")
public void callScheduledMethod() {
getZeroBytesFile();
}

public abstract void getZeroBytesFile();
}

@Component
public class ChildClass extends AbstractTestClass {

@Override
public String getDelay() {
return "5000";
}

@Override
public void getZeroBytesFile() {
System.out.println("called");
}

}

@Component
public class OtherClass {

// This bean initialisation throws error
@AutoWired ChildClass childClass;

}

但是它不起作用并抛出此异常


org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'getDelay': Requested bean is currently in creation: Is there an unresolvable circular reference?

任何指针都会有帮助。提前致谢

最佳答案

这是实现这一目标的技巧:

@EnableScheduling
@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

public abstract class AbstractTestClass {

@Bean
public String getDelay() {
return "1000";
}

@Scheduled(fixedRateString = "#{@getDelay}")
public void callScheduledMethod() {
getZeroBytesFile();
}

public abstract void getZeroBytesFile();
}

@Component
public class ChildClass extends AbstractTestClass {

@Override
public String getDelay() {
return "5000";
}

@Override
public void getZeroBytesFile() {
System.out.println("called");
}

}

关于java - 异常: org. springframework.beans.factory.BeanCurrentlyInCreationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59701817/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com