仿照官网一段代码,网上也都是这么写的,在Spring项目中会有问题
public static voidmain(String[] a) {
ClassPathXmlApplicationContext context =newClassPathXmlApplicationContext("spring/spring.xml");
EchoService echoService = (EchoService) context.getBean("errorService");
String echo = (String) echoService.$echo("ok");
System.out.println("===Scheduled===="+ echo);
}
lassPathXmlApplicationContext context =newClassPathXmlApplicationContext("spring/spring.xml"); 类
会与Spring启动类 @@ImportResource({"classpath*:spring/spring.xml"}) 重复加载导致Duplicate spring bean id
正确示例:
@Component
public classEchoSound {
@Autowired
private ApplicationContextcontext;
@Scheduled(fixedRate =1000)
public voidmain() {
EchoService echoService = (EchoService)context.getBean("errorService");
String echo = (String) echoService.$echo("ok");
System.out.println("===Scheduled===="+ echo);
}
}
异常贴图: