源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JSP 获取spring容器中bean的两种方法总结

  • 时间:2021-07-26 16:56 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JSP 获取spring容器中bean的两种方法总结
[b]JSP 获取spring容器中bean的方法总结[/b] [b]方案1(Web中使用):[/b]
ApplicationContext ct = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
 logService = (ISysLogService) ct.getBean("sysLogServiceImpl");
说明:getRequiredWebApplicationContext方法中需要传入ServletContext()对象,在struts2中可以这样获取:
ServletActionContext.getServletContext()
[b]方案2(通过手动启动spring容器后获取,如果在web中,不建议这样使用):[/b] //匹配多个文件
ApplicationContext ct = new FileSystemXmlApplicationContext(new String[]
 { "classpath:applicationContext.xml","classpath:lb/*/applicationContext-*.xml"});
//加载一个文件
ApplicationContext ct = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
注:获取spring容器中bean的方法还有几个,这里就不一一列举了,个人认为这两个是最直接最简单的。 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部