spring是沒法將bean注入例如ServletContextListene之類的web後臺守護線程的,會提示nullpointer錯誤。而本身new顯然行不通(脫離了spring容器),而再new一個新的spring顯然不是好的辦法,這樣將會有兩個spring,兩套service、dao等組件。較好的辦法是獲取當前惟一的spring容器,再getbean。代碼就兩行:web
WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext(); EmployeeService employeeService=(EmployeeService)context.getBean("employeeService");spring