在一个普通类中使用bean,代码如下:
public class ReceiveThread extends Thread { @Autowired private ApplicationContext ctx; ConcentratorService concentratorService; public void init() { ctx = new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/config/datasource.xml"); concentratorService =(ConcentratorService) ctx.getBean("concentratorService"); } public void run() { String macAddr="12:12:12:12:12:12"; Concentrator c=concentratorService.findConcentratorByCaddress(macAddr); } }
运行多线程之后,service被成功注入了,但是dao为空的。
ConcentratorService中的findConcentratorByCaddress函数如下:
@Service("ConcentratorService") public class ConcentratorServiceImpl implements ConcentratorService{ @Autowired ConcentratorDao concentratorDao; public Concentrator findConcentratorByCaddress(String caddress) throws Exception{ // TODO Auto-generated method stub return concentratorDao.findConcentratorByCaddress(caddress); } }
各位,求救!
注入
ApplicationContext
。一周热门 更多>