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

作者:lqh 时间:2023-06-15 07:27:46 

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

方案1(Web中使用):


ApplicationContext ct = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
logService = (ISysLogService) ct.getBean("sysLogServiceImpl");

说明:getRequiredWebApplicationContext方法中需要传入ServletContext()对象,在struts2中可以这样获取:


ServletActionContext.getServletContext()

方案2(通过手动启动spring容器后获取,如果在web中,不建议这样使用):

//匹配多个文件


ApplicationContext ct = new FileSystemXmlApplicationContext(new String[]
{ "classpath:applicationContext.xml","classpath:lb/*/applicationContext-*.xml"});

//加载一个文件


ApplicationContext ct = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");

注:获取spring容器中bean的方法还有几个,这里就不一一列举了,个人认为这两个是最直接最简单的。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

来源:https://my.oschina.net/ht896632/blog/628440

标签:JSP,spring,bean
0
投稿

猜你喜欢

  • Python实现Kmeans聚类算法

    2023-05-07 19:25:36
  • 在vue中配置不同的代理同时访问不同的后台操作

    2024-05-09 15:08:23
  • eWebEditor 上传文件提示格式不正确的解决方法

    2022-10-08 03:21:51
  • python日志模块loguru详解

    2023-10-23 20:50:08
  • Pandas实现在线文件和剪贴板数据读取详解

    2021-06-02 16:49:55
  • 完美解决vue中报错 “TypeError: Cannot read properties of null (reading'forEach')“

    2023-07-02 17:06:29
  • Python3中的bytes类型和str类型

    2022-06-23 08:14:29
  • jQuery事件的绑定、触发、及监听方法简单说明

    2024-02-24 12:50:53
  • 如何列出SQL数据库中的存储过程?

    2010-01-12 19:58:00
  • Python实战之单词打卡统计

    2023-06-06 18:22:17
  • 教你轻松学会SQL Server记录轮班的技巧

    2009-02-19 17:38:00
  • 最常用的SQL语句

    2024-01-23 20:37:40
  • python批量修改文件名的示例

    2023-11-04 13:50:39
  • Python利用字典破解WIFI密码的方法

    2022-08-08 17:18:44
  • django ManyToManyField多对多关系的实例详解

    2023-08-11 20:21:01
  • Python内存泄漏和内存溢出的解决方案

    2022-02-10 11:46:55
  • pytorch transform数据处理转c++问题

    2023-08-19 11:24:49
  • pandas 获取季度,月度,年度首尾日期的方法

    2022-08-16 06:53:06
  • Python数据分析之 Matplotlib 散点图绘制

    2021-09-19 07:05:46
  • 利用pytorch实现对CIFAR-10数据集的分类

    2021-11-21 03:09:36
  • asp之家 网络编程 m.aspxhome.com