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)

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2021-12-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-05
  • 2021-09-20
  • 2022-12-23
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案