需求:在测试或者其他地方,需要加载service或者mapper的时候,由于该类不是controller类,所以不能用@Autowired直接注入,那么怎么解决呢?
import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); DeptService deptService = wac.getBean(DeptService.class);这样就可以加载。