rethat安装MySQL多例_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取一个别名

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");//配置文件的路径

}

}

65d60dcf384d5052c5a5ea9cd39c6b14.png输出了语句“EmpServiceImp实例化。。。”说明当没有指定是单例模式还是多例模式的时候,默认是单例模式

4、指定单例模式:scope=“singleton”

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");//配置文件的路径

}

}

639390240b0de4e8a66b74b3dcc81656.png结果说明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();

}

}

de35f81d4e1320d4013ffdcf1a30b8a9.png结果实例化了一次,show方法了两次。说明不管获取多少次某个类创建的对象,该实例化对象都只会被创建一次。

5、指定多例模式:scope=“prototype”

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();

}

}

17436f6712cfbec2b21e5e5567cececd.png结果实例化了两次,show方法了两次。说明每次获取的时候都会创建一个新的实例化对象。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值