没有合适的资源?快使用搜索试试~ 我知道了~
Spring获取webapplicationcontext,applicationcontext几种方法详解
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 55 浏览量
2023-10-27
08:47:46
上传
评论
收藏 16KB DOCX 举报
温馨提示
试读
5页
Spring获取webapplicationcontext,applicationcontext几种方法详解
资源推荐
资源详情
资源评论
方法一:在初始化时保存 ApplicationContext 对象
代码:
ApplicationContext ac = new
FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");
说明:这种方式适用于采用 Spring 框架的独立应用程序,需要程序通过配置文件手工初始化
Spring 的情况。
方法二:通过 Spring 提供的工具类获取 ApplicationContext 对象
代码:
import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1 =
WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext
sc);
ApplicationContext ac2 =
WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");
说明:
这种方式适合于采用 Spring 框架的 B/S 系统,通过 ServletContext 对象获取
ApplicationContext 对象,然后在通过它获取需要的类实例。
上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回 null。
其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者
this.getServletContext() 或者 request.getSession().getServletContext(); 另外,由于
spring 是注入的对象放在 ServletContext 中的,所以可以直接在 ServletContext 取出
WebApplicationContext 对象: WebApplicationContext webApplicationContext =
(WebApplicationContext)
servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_C
ONTEXT_ATTRIBUTE);
资源评论
小小哭包
- 粉丝: 1899
- 资源: 3860
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功