Spring中bean的生命周期

来源:互联网 发布:ubuntu安装完重启黑屏 编辑:程序博客网 时间:2024/06/07 00:44

获取bean的方法

1.从ApplicationContex应用上下文容器中获取bean和从bean工厂容器中获取bean

具体案例:

从ApplicationContext中取bean

ApplicationContextac=new ClassPathXmlApplicationContext("com/hsp/ioc/beans.xml");

当我们去实例化beans.xml,该文件中配置的bean被实例(该bean scope是singleton)从bean中取出 ,如果我们使用beanfactory去获取bean,当你只是实例化该容器, 那么容器的bean不被实例化,只有当你去使用getBean某个bean时,才会实时的创建.

 BeanFactory  factory = new XmlBeanFactory(newClassPathResource("com/hsp/ioc/beans.xml"));

factory.getBean("student");

2.结论

a.如果使用ApplicationContext,则配置的bean如果是 singlton不管你用不用,都被实例化.(好处就是可以预先加载,缺点就是耗内存)

b.如果是BeanFactory ,则当你获取beanfacotry时候,配置的bean不会被马上实例化,当你使用的时候,才被实例(好处节约内存,缺点就是速度)

c.规定: 一般没有特殊要求,应当使用ApplicatioContext完成(90%)

bean 的 配置细节


配置文件

<!-- 配置bean的scope生命周期-->

<bean  id="student"  scope="singleton"  class="com.cloud.ioc.Student">

      <propertyname="name" value="张三"/>

</bean>

测试代码

     //获取两个student

             Students1=(Student) ac.getBean("student");

             Students2=(Student) ac.getBean("student");

             System.out.println(s1+""+s2);

 

这里singleton生命周期理解为:在配置文件中,Student类只配置一个bean,也就只对应一个对象,Student s1=(Student) ac.getBean("student");所以这种方式创建的对象是同一个。

request

session

global-session

是在web开发中才有意义.

种获取ApplicationContext 对象

1.      ClassPathXmlApplicationContext->通过类路径

2.      FileSystemXmlApplicationContext->通过文件路径

举例:

ApplicationContext  ac=newFileSystemXmlApplicationContext("文件路径beans.xml/ applicationContext.xml");

3.      XmlWebApplicationContext

使用Bean基本流程

把一个bean纳入到spring IoC容器之中,这个bean的生命周期就会交由容器进行管理

1.Bean的建立
      由BeanFactory读取Bean定义文件,并生成各个实例。
2.Setter注入
      执行Bean的属性依赖注入。
3.BeanNameAware的setBeanName()
      如果Bean类实现了org.springframework.beans.factory.BeanNameAware接口,则执行其setBeanName()方法。
4.BeanFactoryAware的setBeanFactory()
      如果Bean类实现了org.springframework.beans.factory.BeanFactoryAware接口,则执行其setBeanFactory()方法。
5.BeanPostProcessors的processBeforeInitialization()
      容器中如果有实现org.springframework.beans.factory.BeanPostProcessors接口的实例,则任何Bean在初始化之前都会执行这个实例的processBeforeInitialization()方法。
6.InitializingBean的afterPropertiesSet()
      如果Bean类实现了org.springframework.beans.factory.InitializingBean接口,则执行其afterPropertiesSet()方法。
7.Bean定义文件中定义init-method
      在Bean定义文件中使用“init-method”属性设定方法名称,如下:
<bean id="demoBean" class="com.yangsq.bean.DemoBean"init-method="initMethod">
  .......
 </bean>
      这时会执行initMethod()方法,注意,这个方法是不带参数的。
8.BeanPostProcessors的processAfterInitialization()
      容器中如果有实现org.springframework.beans.factory.BeanPostProcessors接口的实例,则任何Bean在初始化之前都会执行这个实例的processAfterInitialization()方法。
9.DisposableBean的destroy()
      在容器关闭时,如果Bean类实现了org.springframework.beans.factory.DisposableBean接口,则执行它的destroy()方法。
10.Bean定义文件中定义destroy-method

     在容器关闭时,可以在Bean定义文件中使用“destory-method”定义的方法

例子:

1.编写PersonService.Java

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;">package com.cloud.beanlife;  
  2. import javax.annotation.PreDestroy;  
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.beans.factory.BeanFactory;  
  5. import org.springframework.beans.factory.BeanFactoryAware;  
  6. import org.springframework.beans.factory.BeanNameAware;  
  7. import org.springframework.beans.factory.InitializingBean;  
  8. import org.springframework.context.ApplicationContext;  
  9. import org.springframework.context.ApplicationContextAware;  
  10. public class PersonService implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean{  
  11.     public PersonService(){  
  12.         System.out.println("对象实例化!");  
  13.     }  
  14.     public PersonService(String abc){  
  15.         System.out.println("对象实例化!");  
  16.     }  
  17.     private String name;  
  18.     private Integer age;  
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.     public void setName(String name) {  
  23.         System.out.println("PersonService.setName()方法被调用");  
  24.         this.name = name;  
  25.     }  
  26.     public Integer getAge() {  
  27.         return age;  
  28.     }  
  29.     public void setAge(Integer age) {  
  30.         this.age = age;  
  31.     }  
  32.     public void sayhello(){  
  33.         System.out.println("hello:"+name+"的年龄是"+age);  
  34.     }  
  35.     //该方法的arg0表示正在被实例化的bean的id 是多少  
  36.     @Override  
  37.     public void setBeanName(String arg0) {  
  38.         System.out.println("PersonService.setBeanName():"+arg0);  
  39.     }  
  40.     //该方法传递BeanFactory  
  41.     @Override  
  42.     public void setBeanFactory(BeanFactory arg0) throws BeansException {  
  43.         System.out.println("PersonService.setBeanFactory():"+arg0);  
  44.     }  
  45.     //该方法可以传递ApplicationContext  
  46.     @Override  
  47.     public void setApplicationContext(ApplicationContext arg0)  
  48.             throws BeansException {  
  49.         System.out.println("PersonService.setApplicationContext():"+arg0);  
  50.     }  
  51.     @Override  
  52.     public void afterPropertiesSet() throws Exception {  
  53.         System.out.println("PersonService.afterPropertiesSet()");  
  54.     }  
  55.     //可以通过注解的方式配置<bean destroy-method="mydestory">销毁方法  
  56.     @PreDestroy  
  57.     public void mydestroy(){  
  58.         System.out.println("生命周期结束,释放各种资源");  
  59.     }  
  60. }  
  61. </span>  
2.编写BeanPostProcess.java
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;">package com.cloud.beanlife;  
  2. import org.springframework.beans.BeansException;  
  3. import org.springframework.beans.factory.config.BeanPostProcessor;  
  4. public class BeanPostProcess implements BeanPostProcessor{  
  5.     @Override  
  6.     public Object postProcessAfterInitialization(Object arg0, String arg1)  
  7.             throws BeansException {  
  8.         System.out.println("postProcessAfterInitialization 函数被调用");  
  9.         System.out.println(arg0+"被创建的 时间是"+new java.util.Date());  
  10.         return arg0;  
  11.     }  
  12.     @Override  
  13.     public Object postProcessBeforeInitialization(Object arg0, String arg1)  
  14.             throws BeansException {  
  15.         System.out.println("postProcessBeforeInitialization 函数被调用");  
  16.         return arg0;  
  17.     }  
  18. }  
  19. </span>  
3.编写配置文件

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  7.         http://www.springframework.org/schema/mvc   
  8.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
  9.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context-3.2.xsd   
  11.         http://www.springframework.org/schema/aop   
  12.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  13.         http://www.springframework.org/schema/tx   
  14.         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  
  15.     <bean id="personService" destroy-method="mydestroy" class="com.cloud.beanlife.PersonService">  
  16.         <property name="name" value="Spring"/>  
  17.         <property name="age">  
  18.         <value>25</value>  
  19.         </property>  
  20.     </bean>  
  21.     <bean id="personService2" class="com.cloud.beanlife.PersonService">  
  22.         <property name="name" value="小明"/>  
  23.     </bean>  
  24.     <!-- 配置自己的处理器,类似于过滤器 -->  
  25.     <bean id="myBeanPostProcessor" class="com.cloud.beanlife.BeanPostProcess">  
  26.     </bean>  
  27. </beans></span>  

4.编写测试文件

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;">package com.cloud.beanlife;  
  2. import org.springframework.context.ApplicationContext;  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4. public class MyTest {  
  5.     /** 
  6.      * 测试大小写字母的转换 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");  
  10.         PersonService ps = (PersonService) ac.getBean("personService");  
  11.         ps.sayhello();  
  12.     }  
  13. }</span>  
0 0
原创粉丝点击