1.在spring中启动线程
Thread thread = new Thread(new AutoRun ());
thread.start();
2.线程类中用到了spring进行DI注入 sqlService
public class AutoRun implements Runnable {
@Resource
private SqlService sqlService;
public void setSqlService(SqlService sqlService) {
this.sqlService = sqlService;
}
public void run() {
while (true) {
try {
String a = new Date().toLocaleString();
sqlService.read("select '"+a+"' from dual");
System.out.println(a);
Thread.sleep(100);// 休眠2分钟
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
3.会报空指针错误,
4.只能通过该类的构造函数从调用类中传过来,或者
//得到容器
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//使用容器调用bean
RagBady bady=(RagBady)context.getBean("rag");