Spring手动获取bean

阅读更多

1.在普通java类中手动获取Spring中的bean

  WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();  //获取application
  SaleRecordService saleRecordService = (SaleRecordService)wac.getBean("saleRecordService");

 

2.在servlet中手动获取Spring中的bean

   WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletcontext);                
  commonManager= (YearCommonManager)ctx.getBean("yearCommonManager"); 

 

你可能感兴趣的:(Spring手动获取bean)