JAVA中SpringMVC获取bean方法,在工具类(utils)注入service

 有时候我们会出现无法用注解 @Autowired 注入bean的情况,这个时候可以

通过contextLoader获取

 

WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
BeanService beanService = (BeanService)ctx.getBean("beanID");

  

 

这里的beanID为xml文件中对应的<bean id="beanID">

 

posted @ 2020-03-29 02:29  yvioo  阅读(1635)  评论(0编辑  收藏  举报