Spring学习笔记二

多种方法实例化Bean

  在我们通过XML配置Bean的过程中其实我们可以使用多种的初始化办法来获取我们的Bean实例,基本上可以分为以下的三种

1、使用类构造器

2、使用静态工厂方法

3、使用实例工厂方法

      对应于以上的三种情况,我们在配置的时候会可以出现三种配置的方法

<!-- 构造方法构造 -->
<bean id = "2stBean_1" class = "test.bean.The2stBean"></bean>
<!-- 静态工厂方法构造 -->
<bean id = "2stBean_2" class = "test.bean.The2stFactory" factory-method="staticGetBean"></bean>
<!-- 实例工厂方法构造 -->
<bean id = "factory_2" class = "test.bean.The2stFactory"></bean>
<bean id = "2stBean_3" factory-bean="factory_2" factory-method="getBean"></bean>

然后列出我们这次实验使用的Bean类以及工厂方法类

package test.bean;

public class The2stBean {
	public The2stBean(){
		System.out.println("The2stBean 构造方法调用.....");
	}

	public void test(){
		System.out.println("this is the2stBean test");
	}
}
package test.bean;

public class The2stFactory {

	public static The2stBean staticGetBean(){
		System.out.println("静态工厂方法调用中......");
		return new The2stBean();
	}
	
	public  The2stBean getBean(){
		System.out.println("实例工厂方法调用中......");
		return new The2stBean();
	}
}

最后使我们这次实验的测试方法

@Test
	public void test003(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config.xml");  
		System.out.println("获取Bean实例开始............");
		The2stBean bean01 = (The2stBean)ctx.getBean("2stBean_1");
		The2stBean bean02 = (The2stBean)ctx.getBean("2stBean_2");
		The2stBean bean03 = (The2stBean)ctx.getBean("2stBean_3");
		bean01.test();
		bean02.test();
		bean03.test();
	}

我们的实验结果

Bean created

The2stBean 构造方法调用.....

静态工厂方法调用中......

The2stBean 构造方法调用.....

实例工厂方法调用中......

The2stBean 构造方法调用.....

获取Bean实例开始............

this is the2stBean test

this is the2stBean test

this is the2stBean test

Bean节点初始化、单例属性配置、延迟加载

    我们之前发现我们的Bean生成的时候是单例的,并且是在获取配置的时候已经加载完毕的,但是作为一个比较优秀的框架,单一的模式是不太适合的,所以在spring中我们可以通过一些参数来改变我们之前了解到的这个种种的情况

    这里我仅介绍init-method(初始化方法)lazy-init(延迟加载)scope(单例模式)这几种。

看以下的这个例子,配置:


<bean id = "2stBean_1" class = "test.bean.The2stBean"  lazy-init="true" init-method="init" scope="prototype"></bean>
<bean id = "2stBean_4" class = "test.bean.The2stBean"  lazy-init="true" init-method="init" ></bean>

然后这个是我们的Bean

package test.bean;

public class The2stBean {
	public The2stBean(){
		System.out.println("The2stBean 构造方法调用.....");
	}

	public void test(){
		System.out.println("this is the2stBean test");
	}
	
	public void init(){
		System.out.println(" this is the2stBean init ");
	}
}

之后是我们的测试方法,我们可以发现我们这些两个Bean都采用了延迟加载的方式,这个使得我们的Bean不论是单例与否都是在我们获取的时候才被加载出来的。

@Test
	public void test004(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config.xml");  
		System.out.println("获取Bean实例开始............");
		The2stBean bean01 = (The2stBean)ctx.getBean("2stBean_1");
		The2stBean bean02 = (The2stBean)ctx.getBean("2stBean_1");
		System.out.println(" bean01 == bean02 = "+(bean01 == bean02));
		The2stBean bean03 =  (The2stBean)ctx.getBean("2stBean_4");
		The2stBean bean04 =  (The2stBean)ctx.getBean("2stBean_4");
		System.out.println(" bean03 == bean04 = "+(bean03 == bean04));
	}

实验结果:

获取Bean实例开始............

The2stBean 构造方法调用.....

 this is the2stBean init

The2stBean 构造方法调用.....

 this is the2stBean init

 bean01 == bean02 = false

The2stBean 构造方法调用.....

 this is the2stBean init

 bean03 == bean04 = true






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值