springmvc获取bean

1、普通注解方式获取

2、springcontextholder获取

ChatHistoryService chatHistoryService = SpringContextHolder.getBean("chatHistoryService");

3、初始化时保存applicationcontext

ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); 
ac.getBean("beanId");

4、通过工具类获取去applicationcontext

ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc); 
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); 
ac1.getBean("beanId"); 
ac2.getBean("beanId");

5、通过contextLoader获取

WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
wac.getBean(beanID)
原文地址:https://www.cnblogs.com/dashuai01/p/6704284.html