Spring Bean的管理

目录 
1.spring 实例化 bean 的几种方式 
2.spring 中bean 的作用域 
3.spring 管理的bean 在什么时候初始化 和 销毁 
4.spring bean 的 init-method  destroy-method 

1.下面 我们来看看spring的 实例化bean的 几种方式  
1.使用类构造器实例化bean 
[xml]  view plain  copy
  1.  <!--1.使用类构造器实例化bean--->  
  2. <bean id="personService" class="com.person.service.impl.PersonServiceBean">  
  3. </bean>  


2 使用 静态工厂方法来实例化 
[xml]  view plain  copy
  1. <!-- 2.使用 静态工厂方法来实例化 bean -->  
  2.     <bean id="personService2" class="com.person.service.impl.PerssonServiceFactory" factory-method="createPersonService"/>  
  3.       

[java]  view plain  copy
  1. package com.person.service.impl;  
  2.   
  3. import com.person.service.PersonService;  
  4.   
  5. public class PerssonServiceFactory {  
  6.     //使用这个工厂中的静态方法  
  7.     public static PersonService createPersonService(){  
  8.         return new PersonServiceBean();  
  9.     }  
  10. }  


3. 使用 实例工厂方式来实例化bean 
[xml]  view plain  copy
  1. <!-- 使用 实例工厂方式来实例化bean -->  
  2.     <bean id="perssonServiceFactory" class="com.person.service.impl.PerssonServiceFactory"/>  
  3.     <bean id="personService3" factory-bean="perssonServiceFactory" factory-method="createPersonService2"/>  


[java]  view plain  copy
  1. package com.person.service.impl;  
  2.   
  3. import com.person.service.PersonService;  
  4.   
  5. public class PerssonServiceFactory {  
  6.       
  7.     //使用 静态工厂方法来实例化 bean  
  8.     public static PersonService createPersonService(){  
  9.         return new PersonServiceBean();  
  10.     }  
  11.       
  12.     //使用 实例工厂方式来实例化bean  
  13.     public PersonService createPersonService2(){  
  14.         return new PersonServiceBean();  
  15.     }  
  16. }  

//测试 
[java]  view plain  copy
  1. @Test  
  2. public void init3(){  
  3.     ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  
  4.       
  5.       
  6.     //方法一  获取服务层对象  
  7.     PersonService personService=(PersonService)ctx.getBean("personService2");  
  8.     //方法二  
  9.     PersonService personService1=ctx.getBean("personService3",PersonService.class);  
  10.       
  11.     personService.save();  
  12.     personService1.save();  
  13. }  


2.弄了这么多 那么spring 中每次 getBean();这个方法返回的对象  是同一个吗??  
[java]  view plain  copy
  1. @Test //测试 spring 容器中的 bean 的作用域  
  2.     public void init4(){  
  3.         ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  
  4.         //方法一  获取服务层对象  
  5.         PersonService personService=(PersonService)ctx.getBean("personService");  
  6.         PersonService personService1=(PersonService)ctx.getBean("personService");  
  7.         //方法二  
  8.         PersonService personService2=ctx.getBean("personService",PersonService.class);  
  9.           
  10.         System.out.println(personService==personService1); //true  
  11.         System.out.println(personService1==personService2); //true   
  12.            
  13.     }  

//以上 说明 是同一个对象 ,说明了spring 默认 的bean 是 单例模式 Singleton 

spring bean 的作用域 scope  

Singleton 
[java]  view plain  copy
  1. 在每个Spring IOC容器中一个bean的定义只有一个对象实例,默认情况下,会在容器服务器启动时初始化bean,但我们可以制定Bean 节点 lazy-init="true" 来延迟初始化bean,这个时候,只有第一个获取bean才会初始化bean.如:  
  2. <bean id="XXX" class="com.person.serivce.impl.PersonServiceBean" lazy-init="true"/>  
  3. 如果 相对 spring文件中的所有bean 都 延迟初始化,可以在根节点beans 设置  default-lazy-init="true" 如下:  
  4. <beans  default-init="true"/>  


prototype:每次从容器获取的bean 都是一个新的对象. (原型) 

//对于 web 应用来说 
request 
session 
global session 

//修改spring 配置 来测试 
[java]  view plain  copy
  1. <!-- 使用 静态工厂方法来实例化 bean 加入 scope 范围-->    
  2.     <bean id="personService2" class="com.person.service.impl.PerssonServiceFactory" factory-method="createPersonService" scope="prototype"/>  

[java]  view plain  copy
  1. @Test //测试 spring 容器中的 bean 的作用域  
  2.     public void init5(){  
  3.         ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  
  4.         //方法一  获取服务层对象  
  5.         PersonService personService=(PersonService)ctx.getBean("personService2");  
  6.         PersonService personService1=(PersonService)ctx.getBean("personService2");  
  7.         //方法二  
  8.         PersonService personService2=ctx.getBean("personService2",PersonService.class);  
  9.           
  10.         System.out.println(personService==personService1); //false  
  11.         System.out.println(personService1==personService2); //false   
  12.     }  

//说明 设置 scope="prototype" 后变成了 原型  每次 获取都是不同的对象 

3.看看 spring 在什么时候 初始化bean  (在 配置 的bean对应的类中 加个 构造方法 就可以测试了)  

1 先测试  singleton 范围 
[java]  view plain  copy
  1. package com.person.service.impl;  
  2.   
  3. import com.person.service.PersonService;  
  4.   
  5. public class PersonServiceBean implements PersonService {  
  6.       
  7.     //测试 spring 在什么时候初始化 bean  
  8.     public PersonServiceBean() {  
  9.         System.out.println("我被实例化了!");  
  10.     }  
  11.       
  12.     public void save(){  
  13.         System.out.println("我就是save方法!");  
  14.     }  
  15. }  

[java]  view plain  copy
  1. @Test //测试 spring的 初始化  
  2.     public void init(){  
  3.         ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  
  4.           
  5.     }  

// 发现 打印 出了 "我被实例化了!"  说明 spring 默认情况下 是加载 spring配置文件的时候就初始化了 bean 

2.在来测试 scope="prototype" 
[java]  view plain  copy
  1. @Test //测试 spring的 初始化  
  2. public void init(){  
  3.     ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  
  4.       
  5.     System.out.println("开始调用 getBean方法");  
  6.     //方法一  获取服务层对象  
  7.     PersonService personService=(PersonService)ctx.getBean("personService");  
  8.     //方法二  
  9.     PersonService personService1=ctx.getBean("personService",PersonService.class);  
  10.       
  11.       
  12.     //personService.save();  
  13.     //personService1.save();  
  14. }  


// 发现 打印出 "开始调用 getBean方法" 之后 才打印出 "我被实例化了!"  "我被实例化了!" 
// 说明 scope="prototype" 是在 getBean方法后  才被实例化 

3.再来看延迟初始化 lazy-init="true"; 
[xml]  view plain  copy
  1. <!-- 使用 延迟初始化  -->  
  2. <bean id="personService" class="com.person.service.impl.PersonServiceBean" lazy-init="true">  
  3. </bean>  


[java]  view plain  copy
  1. @Test //测试 spring的 初始化  
  2. public void init(){  
  3.     ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  
  4.       
  5.     System.out.println("开始调用 getBean方法");  
  6.     //方法一  获取服务层对象  
  7.     PersonService personService=(PersonService)ctx.getBean("personService");  
  8.     //方法二  
  9.     PersonService personService1=ctx.getBean("personService",PersonService.class);  
  10.       
  11.       
  12.     //personService.save();  
  13.     //personService1.save();  
  14. }  

// 发现 打印出 "开始调用 getBean方法" 之后 才打印出 "我被实例化了!"  

说明 lazy-init="true" 也是在 getBean方法后才被初始化   但是 使用的是同一个对象 所以 只打印出了一句 "我被实例化了!"  .而scope="prototype" 是原型 所以 打印出了 两句  "我被实例化了!" . 

4.如果 我们想在 spring 初始化bean实例后  执行一个方法 , 或者 在  bean销毁后 执行一个方法 (比如 资源的打开  和 使用完后 资源的关闭:写到这 大家是不是 就想到了 打开数据库连接 和 关闭呀)  

[java]  view plain  copy
  1. package com.person.service.impl;  
  2.   
  3. import com.person.service.PersonService;  
  4.   
  5. public class PersonServiceBean implements PersonService {  
  6.       
  7.     //初始化后 执行的方法  
  8.     public void init(){  
  9.         System.out.println("打开数据库的连接");  
  10.     }  
  11.       
  12.     //销毁后执行的方法  
  13.     public void destroy(){  
  14.         System.out.println("关闭 数据的连接资源");  
  15.     }  
  16.       
  17.     //测试 spring 在什么时候初始化 bean  
  18.     public PersonServiceBean() {  
  19.         System.out.println("我被实例化了!");  
  20.     }  
  21.       
  22.     public void save(){  
  23.         System.out.println("我就是save方法!");  
  24.     }  
  25. }  


使用 这两个属性  init-method  , destroy-method 
[xml]  view plain  copy
  1. <!-- 使用个 实例工厂方式来实例化bean -->  
  2.     <bean id="perssonServiceFactory" class="com.person.service.impl.PerssonServiceFactory"/>  
  3.     <bean id="personService3" factory-bean="perssonServiceFactory" factory-method="createPersonService2" init-method="init" destroy-method="destroy"/>  


[java]  view plain  copy
  1. @Test  //测试 spring 的 init-method  , destroy-method  
  2.     public void init6(){  
  3.         //注意这里  ApplicationContext 接口 没有 close 方法 只有 用  父接口   
  4.         AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  
  5.         //方法二  
  6.         PersonService personService1=ctx.getBean("personService3",PersonService.class);  
  7.         personService1.save();  
  8.         ctx.close();   
  9.     }  

// 发现  init 和 destroy 方法 都执行了 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值