spring心得2--bean的生命周期@Spring监听器的作用@Spring初始化容器案例分析@web项目使用...

本文详细介绍了Spring容器的初始化过程,包括Bean的生命周期、容器启动配置、不同方式加载配置文件及其实现原理。同时通过示例代码展示了如何在Web环境中配置和使用Spring。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Scope的默认值是singleton,lazy-init的默认值是default,default相当于false

1.bean的生命周期

bean被载入到容器中时,他的生命周期就开始了。bean工厂在一个bean可以使用前完成很多工作:

1).容器寻找bean的定义信息并实例化。

2).使用依赖注入,spring按bean定义信息配置bean的所有属性。

3).若bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法传递bean的ID。

4).若bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身。

5).若BeanPostProcessor和bean关联,则它们的 postProcessBeforeInitialization()方法被调用。

6).若bean指定了ini-method方法、,它将被调用。

7).最后,若有BeanPostProcessor和bean关联,则它们的postProcessAfterInitialization()方法被调用。

2.Spring监听器的作用

我们自动加载spring配置文件需要在web.xml文件中添加一段配置:

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value> classpath*:applicationContext-*.xml </param-value>

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。至于ApplicationContext.xml这个配置文件部署在哪,如何配置多个xml文件,查看它的API文档,在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。

查看API得知:ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。如果查看ContextLoaderServlet的API,可以看到它也关联了ContextLoader这个类而且它实现了HttpServlet这个接口;

ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext-> BeanFactory这样一来spring中的所有bean都由这个类来创建。

如何部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

/WEB-INF/classes/applicationContext-*.xml

</param-value>

</context-param>

在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔。上面的applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入

注意:

如果beans.xml文件放在src目录下,在生成ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");但若是applicationContext.xml必须放到WEB-INF下的Classes目录下,才能用ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");生成ApplicationContext,若是直接放到WEB-INF下会抛出异常说找不到applicationContext文件

3. Spring初始化容器 案例分析

  1. <spanstyle="font-family:KaiTi_GB2312;font-size:18px;">packagewww.csdn.spring.test;
  2. importjava.beans.XMLDecoder;
  3. importorg.junit.Test;
  4. importorg.springframework.beans.factory.BeanFactory;
  5. importorg.springframework.beans.factory.xml.XmlBeanFactory;
  6. importorg.springframework.context.ApplicationContext;
  7. importorg.springframework.context.support.ClassPathXmlApplicationContext;
  8. importorg.springframework.context.support.FileSystemXmlApplicationContext;
  9. importorg.springframework.core.io.ClassPathResource;
  10. importorg.springframework.core.io.FileSystemResource;
  11. importwww.csdn.spring.service.HelloService;
  12. importwww.csdn.spring.service.impl.HelloServiceImpl;
  13. publicclassFactoryTest{
  14. @Test
  15. publicvoidtestFacory(){
  16. /*
  17. *Spring初始化容器.三种经常用到的实现:
  18. *一、ClassPathXmlApplicationContext:从类路径中加载。
  19. *二、FileSystemXmlApplicationContext:从文件系统加载。
  20. *三、XmlWebApplicationContext:从web系统中加载。InputStreamResource不能够使用了。
  21. *特殊的例子:不能使用InputStreamResource
  22. *InputStreamResourceres=newInputStreamResource(new
  23. *FileInputStream("D:\\Ncode\\mcode\\sday02\\src\\spring.xml"));
  24. *
  25. *isOpen(){returntrue;}
  26. */
  27. //下面所列举的案例都是使用bean工厂加载配置文件的几种案例,使用上下文的案例在上一篇博客的springHelloJava案例中已经介绍过了
  28. /*//方法一:FileSystemResource类的使用
  29. FileSystemResourcefileResource=newFileSystemResource(
  30. "F:\\csdn-study\\MyWorkspace\\springHelloJava\\src\\spring.xml");
  31. //方法二:ClassPathResource类的使用
  32. ClassPathResourceclassResource=newClassPathResource("spring.xml");
  33. BeanFactoryfactory=(BeanFactory)newXmlBeanFactory(classResource);
  34. */
  35. //方法三:FileSystemXmlApplicationContext与ClassPathXmlApplicationContext的对比案例
  36. //BeanFactoryfactory=newFileSystemXmlApplicationContext("F:\\csdn-study\\MyWorkspace\\springHelloJava\\src\\spring.xml");
  37. ApplicationContextfactory=newClassPathXmlApplicationContext("classpath:spring.xml");
  38. HelloServicehelloservice=(HelloService)factory
  39. .getBean("helloServiceImpl",HelloServiceImpl.class);
  40. helloservice.sayHello();
  41. }
  42. }
  43. 4.web项目使用spring的入门案例
  44. 一下配置文件和类关键还是看注释,注释中有spring的注意事项以及犀利的知识点
  45. spring.xml
  46. <?xmlversion="1.0"encoding="UTF-8"?>
  47. <beansxmlns="http://www.springframework.org/schema/beans"
  48. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49. xsi:schemaLocation="http://www.springframework.org/schema/beans
  50. http://www.springframework.org/schema/beans/spring-beans.xsd">
  51. <!--spring容器就是负责创建、管理、维护Bean并且能够依赖注入到相应组件上-->
  52. <beanid="helloDaoImpl"class="www.csdn.spring.dao.impl.HelloDaoImpl"scope="singleton"lazy-init="default"></bean>
  53. </beans>
  54. Web.xml配置文件
  55. <?xmlversion="1.0"encoding="UTF-8"?>
  56. <web-appversion="2.5"
  57. xmlns="http://java.sun.com/xml/ns/javaee"
  58. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  60. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  61. <display-name></display-name>
  62. <context-param>
  63. <param-name>contextConfigLocation</param-name>
  64. <!--这里的classpath路径是spring.xml在src路径下
  65. <param-value>classpath:spring.xml</param-value>
  66. -->
  67. <!--也可以像下面这种写法-->
  68. <param-value>/WEB-INF/classes/www/csdn/spring/resource/spring.xml</param-value>
  69. </context-param>
  70. <listener>
  71. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  72. </listener>
  73. <servlet>
  74. <servlet-name>HelloServlet</servlet-name>
  75. <servlet-class>www.csdn.spring.servlet.HelloServlet</servlet-class>
  76. </servlet>
  77. <servlet-mapping>
  78. <servlet-name>HelloServlet</servlet-name>
  79. <url-pattern>/servlet/HelloServlet</url-pattern>
  80. </servlet-mapping>
  81. <welcome-file-list>
  82. <welcome-file>index.jsp</welcome-file>
  83. </welcome-file-list>
  84. </web-app>
  85. HelloServlet类
  86. packagewww.csdn.spring.servlet;
  87. importjava.io.IOException;
  88. importjavax.servlet.ServletException;
  89. importjavax.servlet.http.HttpServlet;
  90. importjavax.servlet.http.HttpServletRequest;
  91. importjavax.servlet.http.HttpServletResponse;
  92. importorg.springframework.web.context.WebApplicationContext;
  93. importorg.springframework.web.context.support.WebApplicationContextUtils;
  94. importorg.springframework.web.context.support.XmlWebApplicationContext;
  95. importwww.csdn.spring.dao.HelloDao;
  96. importwww.csdn.spring.dao.impl.HelloDaoImpl;
  97. publicclassHelloServletextendsHttpServlet{
  98. publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
  99. throwsServletException,IOException{
  100. doPost(request,response);
  101. }
  102. publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
  103. throwsServletException,IOException{
  104. /*//1.利用XmlWebApplicationContext对象加载
  105. XmlWebApplicationContextcontext=newXmlWebApplicationContext();
  106. *默认的路径/WEB-INF/applicationContext.xml,applicationContext.xml文件名称
  107. *可以任意起,比如我改成了spring.xml;
  108. *位置也可以随便放,但是如果放到src下路径就应该写成/WEB-INF/classes/applicationContext.xml;
  109. *放到某一包下,路径就应该写成对应classes加上包名,比如:
  110. //加载spring文件
  111. context.setConfigLocation("/WEB-INF/classes/www/csdn/spring/resource/spring.xml");
  112. //将servlet的上下文环境设置成spring的上下文环境
  113. context.setServletContext(getServletContext());
  114. //刷新容器
  115. context.refresh();
  116. HelloDaohelloDao=(HelloDao)context.getBean("helloDaoImpl");
  117. helloDao.sayHello();
  118. //XmlWebApplicationContextcontext=newXmlWebApplicationContext();
  119. System.out.println("=======================");*/
  120. //2.利用WebApplicationContextUti对象加载
  121. //使用这种方式,spring不会默认的额加载spring配置文件,也不能像上面那种写法一样去写,这时要在web.xml配置一个监听器,写一个配置标签来指定spring配置文件的位置
  122. WebApplicationContextcontexts=WebApplicationContextUtils
  123. .getWebApplicationContext(getServletContext());
  124. HelloDaohelloDaos=contexts.getBean("helloDaoImpl",HelloDaoImpl.class);
  125. helloDaos.sayHello();
  126. }
  127. }
  128. </span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值