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

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

通过contextLoader获取

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

  

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

猜你喜欢

转载自www.cnblogs.com/pxblog/p/12590653.html