JSP获取spring 的容器ApplicationContext

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:

Jsp 中如何获取spring的bean呢?

方式一:通过上下文

 

Html代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  3.     pageEncoding="UTF-8"%>  
  4. <%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>  
  5. <%@page import="org.springframework.context.ApplicationContext"%>  
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head>  
  9. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  10. <title>Insert title here</title>  
  11. </head>  
  12. <body>  
  13. <%  
  14. ServletContext context = request.getSession().getServletContext();  
  15. ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);  
  16. Object <span style="font-size: 1em; line-height: 1.5;">supermarketDao</span><span style="font-size: 1em; line-height: 1.5;">= ctx.getBean("supermarketDao");</span>  
  17. System.out.println("jsp:"+<span style="font-size: 1em; line-height: 1.5;">supermarketDao</span><span style="font-size: 1em; line-height: 1.5;">);</span>  
  18. %>  
  19. </body>  
  20. </html>  

 问题:JSP页面中获取的bean与spring 容器中的bean是同一个吗?

 

是的

 

方式二:通过类路径加载bean文件,得到bean工厂

 

Html代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <%@page import="org.springframework.beans.factory.BeanFactory"%>  
  3. <%@page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>  
  4. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  5.     pageEncoding="UTF-8"%>  
  6. <%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>  
  7. <%@page import="org.springframework.context.ApplicationContext"%>  
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  9. <html xmlns="http://www.w3.org/1999/xhtml">  
  10. <head>  
  11. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  12. <title>Insert title here</title>  
  13. </head>  
  14. <body>  
  15. <%  
  16. ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml","user_beans.xml","goods_beans.xml","supermarket_beans.xml","aop.xml","upload_beans.xml");   
  17. BeanFactory factory = (BeanFactory) appContext;   
  18. Object obj=factory.getBean("supermarketDao");  
  19. System.out.println("jsp2:"+obj);  
  20.   
  21. %>  
  22. </body>  
  23. </html>  

  问题:JSP页面中获取的bean与spring 容器中的bean是同一个吗?

 

是的

 

总结:(1)通过WebApplicationContextUtils 获取bean是直接从spring容器中拿的;

(2)通过ClassPathXmlApplicationContext,实际上又解析了一遍xml,即又创建了一个新的spring容器,所有获取的bean与web上下文中是不同的。

相关文章
|
2月前
|
XML Java 数据格式
编织Spring魔法:解读核心容器中的Beans机制【beans 一】
编织Spring魔法:解读核心容器中的Beans机制【beans 一】
42 0
|
1月前
|
Java 测试技术 数据库连接
【Spring源码解读!底层原理高级进阶】【下】探寻Spring内部:BeanFactory和ApplicationContext实现原理揭秘✨
【Spring源码解读!底层原理高级进阶】【下】探寻Spring内部:BeanFactory和ApplicationContext实现原理揭秘✨
|
2天前
|
XML Java 数据格式
【spring】01 Spring容器研究
【spring】01 Spring容器研究
7 0
|
1月前
|
Java 容器 Spring
【spring(一)】核心容器总结
【spring(一)】核心容器总结
|
1月前
|
Java 开发者 容器
【Java】深入了解Spring容器的两个关键组件
【Java】深入了解Spring容器的两个关键组件
10 0
|
1月前
|
XML Java 数据格式
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界 (下)
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界
|
1月前
|
XML Java 数据格式
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界 (上)
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界 (上)
|
1月前
|
XML Java 开发者
【Spring源码解读 底层原理高级进阶】【上】探寻Spring内部:BeanFactory和ApplicationContext实现原理讲解
【Spring源码解读 底层原理高级进阶】【上】探寻Spring内部:BeanFactory和ApplicationContext实现原理讲解
|
2月前
|
前端开发 Java 数据格式
10个知识点让你读懂spring MVC容器
随着 Spring Boot 逐步全面覆盖到我们的项目之中,我们已经基本忘却当年经典的 Servlet + Spring MVC 的组合,那让人熟悉的 web.xml 配置。而本文,我们想先抛开 Spring Boot 到一旁,回到从前,一起来看看 Servlet 是怎么和 Spring MVC 集成,怎么来初始化 Spring 容器的。
20 1
|
25天前
|
Java
学校教师管理系统【JSP+Servlet+JavaBean】(Java课设)
学校教师管理系统【JSP+Servlet+JavaBean】(Java课设)
20 1