Spring框架-Bean作用域中单例模式和多例模式的区别

Spring框架-Bean作用域中单例模式和多例模式的区别

一、单例模式的特点(当没有指定是单例模式还是多例模式的时候,默认是单例模式):
1、Spring容器创建的时候,对应的类的实例化对象一起被创建。
2、不管获取多少次某个类创建的对象,该实例化对象都只会被创建一次。
二、多例模式的特点:
1、Spring容器创建的时候,对应的类的实例化对象不会被创建,只有在被获取的时候才会被创建。
2、每次获取的时候都会创建一个新的实例化对象。
三、下面通过一个案例来演示这两种模式的区别
1、新建一个普通类,包含无参构造方法和有参构造方法,还有一个展示对象信息的方法。

package com.qst.service;

public class EmpServiceImp {
	private int id=1;
	private String name="张倩";
	private String address="浙江杭州";
		
	public EmpServiceImp() {
		System.out.println("EmpServiceImp实例化。。。");
	}

	public EmpServiceImp(int id, String name, String address) {
		this.id = id;
		this.name = name;
		this.address = address;
	}


	public void showEmp() {
		System.out.println(id+"--"+name+"--"+address);
	}
}

2、在applicationContext.xml文件中写一个没有指定单例还是多例模式的bean,可以给该bean取一个别名

<bean id="service" class="com.qst.service.EmpServiceImp"/>
<!-- 取别名 -->
     <alias name="service" alias="ser"/>

3、写一个测试方法

package com.qst.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qst.service.EmpServiceImp;

public class AppContextTest {

	public static void main(String[] args) {
		//创建Spring容器
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//配置文件的路径
	}
}

在这里插入图片描述输出了语句“EmpServiceImp实例化。。。”说明当没有指定是单例模式还是多例模式的时候,默认是单例模式
4、指定单例模式:scope=“singleton”

<bean id="service" class="com.qst.service.EmpServiceImp" scope="singleton"/>
     <!-- 取别名 -->
     <alias name="service" alias="ser"/>
package com.qst.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qst.service.EmpServiceImp;

public class AppContextTest {

	public static void main(String[] args) {
		//创建Spring容器
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//配置文件的路径		 
	}
}

结果说明Spring容器创建的时候,对应的类的实例化对象一起被创建。

package com.qst.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qst.service.EmpServiceImp;

public class AppContextTest {

	public static void main(String[] args) {
		//创建Spring容器
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//配置文件的路径
	    //获取类创建的对象				
		 EmpServiceImp service=(EmpServiceImp)ac.getBean("ser"); 
		 service .showEmp();
		 EmpServiceImp service2=(EmpServiceImp)ac.getBean("ser"); 
		 service2 .showEmp();	 
	}
}

在这里插入图片描述结果实例化了一次,show方法了两次。说明不管获取多少次某个类创建的对象,该实例化对象都只会被创建一次。
5、指定多例模式:scope=“prototype”

<bean id="service" class="com.qst.service.EmpServiceImp" scope="prototype"/>
     <!-- 取别名 -->
     <alias name="service" alias="ser"/>
package com.qst.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qst.service.EmpServiceImp;

public class AppContextTest {

	public static void main(String[] args) {
		//创建Spring容器
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//配置文件的路径
	    //获取类创建的对象	
		 EmpServiceImp service=(EmpServiceImp)ac.getBean("ser"); 
		 service .showEmp();
		 EmpServiceImp service2=(EmpServiceImp)ac.getBean("ser"); 
		 service2 .showEmp();		 
	}
}

在这里插入图片描述结果实例化了两次,show方法了两次。说明每次获取的时候都会创建一个新的实例化对象。

猜你喜欢

转载自blog.csdn.net/weixin_43965383/article/details/109123362