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)

 

posted @ 2017-04-13 15:54  ordinary01  阅读(1651)  评论(0)    收藏  举报