首页 > 技术文章 > JAVA中SpringMVC获取bean方法,在工具类(utils)注入service

pxblog 2020-03-29 02:29 原文

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

通过contextLoader获取

 

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

  

 

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

 

推荐阅读