Spring Bean的管理

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

[b][color=red]1.下面 我们来看看spring的 实例化bean的 几种方式[/color][/b]
1.使用类构造器实例化bean

<!--1.使用类构造器实例化bean--->
<bean id="personService" class="com.person.service.impl.PersonServiceBean">
</bean>


2 使用 静态工厂方法来实例化

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



package com.person.service.impl;

import com.person.service.PersonService;

public class PerssonServiceFactory {
//使用这个工厂中的静态方法
public static PersonService createPersonService(){
return new PersonServiceBean();
}
}


3. 使用 实例工厂方式来实例化bean

<!-- 使用 实例工厂方式来实例化bean -->
<bean id="perssonServiceFactory" class="com.person.service.impl.PerssonServiceFactory"/>
<bean id="personService3" factory-bean="perssonServiceFactory" factory-method="createPersonService2"/>



package com.person.service.impl;

import com.person.service.PersonService;

public class PerssonServiceFactory {

//使用 静态工厂方法来实例化 bean
public static PersonService createPersonService(){
return new PersonServiceBean();
}

//使用 实例工厂方式来实例化bean
public PersonService createPersonService2(){
return new PersonServiceBean();
}
}

//测试

@Test
public void init3(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");


//方法一 获取服务层对象
PersonService personService=(PersonService)ctx.getBean("personService2");
//方法二
PersonService personService1=ctx.getBean("personService3",PersonService.class);

personService.save();
personService1.save();
}


[b][color=red]2.弄了这么多 那么spring 中每次 getBean();这个方法返回的对象 是同一个吗??[/color][/b]

@Test //测试 spring 容器中的 bean 的作用域
public void init4(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//方法一 获取服务层对象
PersonService personService=(PersonService)ctx.getBean("personService");
PersonService personService1=(PersonService)ctx.getBean("personService");
//方法二
PersonService personService2=ctx.getBean("personService",PersonService.class);

System.out.println(personService==personService1); //true
System.out.println(personService1==personService2); //true

}

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

[b][color=red]spring bean 的作用域 scope[/color][/b]

Singleton

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


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

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

//修改spring 配置 来测试

<!-- 使用 静态工厂方法来实例化 bean 加入 scope 范围-->
<bean id="personService2" class="com.person.service.impl.PerssonServiceFactory" factory-method="createPersonService" scope="prototype"/>


@Test //测试 spring 容器中的 bean 的作用域
public void init5(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//方法一 获取服务层对象
PersonService personService=(PersonService)ctx.getBean("personService2");
PersonService personService1=(PersonService)ctx.getBean("personService2");
//方法二
PersonService personService2=ctx.getBean("personService2",PersonService.class);

System.out.println(personService==personService1); //false
System.out.println(personService1==personService2); //false
}


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

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

1 先测试 singleton 范围

package com.person.service.impl;

import com.person.service.PersonService;

public class PersonServiceBean implements PersonService {

//测试 spring 在什么时候初始化 bean
public PersonServiceBean() {
System.out.println("我被实例化了!");
}

public void save(){
System.out.println("我就是save方法!");
}
}


@Test //测试 spring的 初始化
public void init(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

}

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

2.在来测试 scope="prototype"


@Test //测试 spring的 初始化
public void init(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

System.out.println("开始调用 getBean方法");
//方法一 获取服务层对象
PersonService personService=(PersonService)ctx.getBean("personService");
//方法二
PersonService personService1=ctx.getBean("personService",PersonService.class);


//personService.save();
//personService1.save();
}


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

3.再来看延迟初始化 lazy-init="true";

<!-- 使用 延迟初始化 -->
<bean id="personService" class="com.person.service.impl.PersonServiceBean" lazy-init="true">
</bean>




@Test //测试 spring的 初始化
public void init(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

System.out.println("开始调用 getBean方法");
//方法一 获取服务层对象
PersonService personService=(PersonService)ctx.getBean("personService");
//方法二
PersonService personService1=ctx.getBean("personService",PersonService.class);


//personService.save();
//personService1.save();
}

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

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

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


package com.person.service.impl;

import com.person.service.PersonService;

public class PersonServiceBean implements PersonService {

//初始化后 执行的方法
public void init(){
System.out.println("打开数据库的连接");
}

//销毁后执行的方法
public void destroy(){
System.out.println("关闭 数据的连接资源");
}

//测试 spring 在什么时候初始化 bean
public PersonServiceBean() {
System.out.println("我被实例化了!");
}

public void save(){
System.out.println("我就是save方法!");
}
}


使用 这两个属性 init-method , destroy-method

<!-- 使用个 实例工厂方式来实例化bean -->
<bean id="perssonServiceFactory" class="com.person.service.impl.PerssonServiceFactory"/>
<bean id="personService3" factory-bean="perssonServiceFactory" factory-method="createPersonService2" init-method="init" destroy-method="destroy"/>



@Test //测试 spring 的 init-method , destroy-method
public void init6(){
//注意这里 ApplicationContext 接口 没有 close 方法 只有 用 父接口
AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//方法二
PersonService personService1=ctx.getBean("personService3",PersonService.class);
personService1.save();
ctx.close();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值