仿照官網(wǎng)一段代碼,網(wǎng)上也都是這么寫的板壮,在Spring項(xiàng)目中會有問題
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"}) 重復(fù)加載導(dǎo)致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);
}
}
異常貼圖: