spring工作中动态加载其他类

需求:在测试或者其他地方,需要加载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);
这样就可以加载。



猜你喜欢

转载自blog.csdn.net/qq_39723363/article/details/80334699