java-Spring-入门学习-第二天(单例模式和多例模式)

目录

 Bean作用域

单例模式(默认可以不写)

Spring下的 @AutoWired 依赖注入

JaveEE下的 @Resource 依赖注入

多例模式


 Bean作用域

​在Spring框架中,Bean是按照作用域来创建的,常见的作用域有两种:Singleton 和 Prototype。Singleton (单例)是指整个应用中只有一个实例,并在第一次请求时创建实例

Prototype (多例)是指每次请求都会创建一个新的实例并返回,每个实例之间是相互独立的。

@Scope注解指定bean的作用域
取值含义
@Scope("singleton")(默认)在IoC容器中,在bean的对象为单实例
@Scoper("prototype")在IoC容器中,在bean中有多个实例

单例模式(默认可以不写)

Spring下的 @AutoWired 依赖注入

注意的是@Autowired 不能以类的名字来寻找对应的Product的实体类,需要通过@Qualifier来查找对应的接口实体类,不确定对应的实体类的名会报NoUniqueBeanDefinitionException异常。

这个异常表明Spring容器中有多个相同类型的bean候选者,但Spring不知道应该选择哪一个来注入

// 一个接口  
interface Product {  void test();  
}  @Component("productDealWith1") // 使用 @Component 并指定 bean 名称  
class ProductDealWith1 implements Product {  @Override  public void test() {  System.out.println("ProductDealWith1 test method called.");  }  
}  @Component("productDealWith") // 使用 @Component 并指定 bean 名称  
class ProductDealWith implements Product {  @Override  public void test() {  System.out.println("ProductDealWith test method called.");  }  
}  @Component  
class ProductOrder {  @Autowired  @Qualifier("productDealWith1") // 使用 @Qualifier 指定要注入的 bean 名称  private Product product;  public void doSomething() {  product.test(); // 这将调用 ProductDealWith1 的 test 方法  }  
}  public class TestProduct {  public static void main(String[] args) {  ApplicationContext context = new AnnotationConfigApplicationContext("demo.test.product");  ProductOrder order = context.getBean(ProductOrder.class); // 获取 ProductOrder 类型的 bean  order.doSomething(); // 这将间接调用 ProductDealWith1 的 test 方法  }  
}

ProductDealWith1  和 ProductDealWith   类都使用了 @Component 注解,并且分别通过 value 参数指定了它们的 bean 名称。在 ProductOrder 类中,通过@Autowired 和 @Qualifier 注解,我们指定了要注入的  Product 类型的 bean 是名为 "productDealWith1" 的那个。在 TestProduct 的 main 方法中,我们通过 context.getBean(ProductOrder.class) 来获取 ProductOrder 类型的 bean,并调用其 doSomething 方法,这将间接调用ProductDealWith1 的 test方法

JaveEE下的 @Resource 依赖注入

这边讲解一下顺便讲解@Resource依靠name找寻接口的实例

因为@Resource是Java EE 的一部分,如果您指定了 name 属性,Spring 将会查找与指定名称匹配的 bean;如果没有指定name  属性,Spring 将会查找与注入点类型匹配的 bean

@Configueration是将该类转变成配置类

其次使用配置类扫描工具@ComponentScan,使其在运行编译过程中优先加载其类

并根据其中的配置创建并管理相应的 bean

 

 
@Configuration  
@ComponentScan("demo.test.product")  
public class AppConfig {  // 该配置类告诉Spring在此包及其子包中查找带有@Component注解的类  
}
// 一个接口  
interface Product {  void test();  
}  @Component(name= "productDealWith1") // 使用 @Component 并指定 bean 名称  
class ProductDealWith1 implements Product {  @Override  public void test() {  System.out.println("ProductDealWith1 test method called.");  }  
}  @Component(name="productDealWith") // 使用 @Component 并指定 bean 名称  
class ProductDealWith implements Product {  @Override  public void test() {  System.out.println("ProductDealWith test method called.");  }  
}  @Component  
class ProductOrder {  @Resource(name ="productDealWith1") private Product product;  public void doSomething() {  product.test(); // 这将调用 ProductDealWith1 的 test 方法  }  
}  public class TestProduct {  public static void main(String[] args) {  ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);  ProductOrder order = context.getBean(ProductOrder.class); // 获取 ProductOrder 类型的 bean  order.doSomething(); // 这将间接调用 ProductDealWith1 的 test 方法  }  
}

多例模式

//这边是多实例
@Scope(value="prototype")
//这边是单实例
//@Scope(value="singleton")
@Component
class Product{}
public class TestDBConnect {@Testpublic void testScope(){ApplicationContext context = new AnnotationConfigApplicationContext("demo.test.product");// 第一次获取Product product1= context.getBean(product.class);System.out.println(product1);// 第二次获取Product product2 = context.getBean(product.class);System.out.println(product2);}
}

多例模式运行

当为多例模式 prototype 时,多次获取bean实例的地址是不同的

单例模式运行

当为单例模式 singleton 时,多次获取bean实例的地址是相同的

单例模式和多例模式的区别

单例模式适用于需要共享数据并且需要避免重复创建实例的情况。

而多例模式适用于需要动态地创建对象并提供独立实例的情况。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/636809.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

STM32 HAL库F103系列之ADC实验(一)

ADC工作原理: 1、输入通道: 2、转换序列: A/D转换被组织为两组:规则组(常规转换组)和注入组(注入转换组) 规则组最多可以有16个转换,注入组最多有4个转换 规则组和注入…

【GIS面试】GIS算法介绍

作者:后端小肥肠 1. 前言 在地理信息系统(GIS)的领域中,算法扮演着极其重要的角色,它们使得复杂的空间数据分析成为可能。无论是在环境科学、城市规划,还是在灾害管理等众多领域,高效和精确的算…

【信号与系统 - 10】拉普拉斯变换

1 定义 周期信号的傅里叶变换那篇提到了&#xff1a; F ( j w ) ∫ − ∞ ∞ e − j w t f ( t ) d t F(jw)\int^{\infty}_{-\infty}e^{-jwt}f(t)dt F(jw)∫−∞∞​e−jwtf(t)dt 这个定义式需要满足绝对可积&#xff0c;即 ∫ − ∞ ∞ ∣ f ( t ) ∣ d t < ∞ \int…

C++11 数据结构5 队列的概念,队列的顺序存储,实现,测试

一&#xff0c;队列的概念 队列是一种特殊的受限制的线性表。 队列&#xff08;queue&#xff09;是只允许在一端进行插入操作&#xff0c;而在另一端进行删除操作的线性表。 队列是一种先进先出的t&#xff08;First In First Out&#xff09;的线性表&#xff0c;简称FIF…

这8款3DMAX建筑室内插件一个都不能少!

3DMax是一款经典的建筑室内场景设计和渲染软件&#xff0c;它能够帮助3D设计师、建筑师和艺术家实现他们的创意概念。本文推荐的8款建筑室内插件&#xff0c;将使3DMax如虎添翼&#xff0c;大大节约设计师们的工作时间&#xff0c;提高工作效率。 1.3DMAX楼层平面图生成器&…

idea使用plantuml插件报错(类图):Dot Executable: /opt/local/bin/dot

报错提示&#xff1a; 解决方式&#xff1a; 方式一: 直接设置Remote Rendering即可 &#xff08;使用服务器地址&#xff09; 无特殊要求可直接使用默认提供的服务地址&#xff0c;也可自行搭建服务替换地址。 自行搭建服务可参考&#xff1a; 在本地Windows 11 系统的桌面…

C++ 程序的内存分配

C 程序的内存分配 C 程序的内存分配栈堆数据区程序代码区参考 C 程序的内存分配 一个 C 编译的程序占用内存分为以下几个部分&#xff08;从高地址到低地址&#xff09;&#xff1a; 内核空间&#xff1a;由操作系统创建并控制&#xff0c;用户代码不能读写。栈&#xff1a;由…

【ds】替换空格

用‘%20’替换空格 var replaceBlank (charArr)> {if (!charArr || charArr.length 0) return var len charArr.lengthlet spaceLen 0for (let i 0; i < len; i) {if (charArr[i] ) {spaceLen}}var extraLen spaceLen * 2 // -> 20% 每一个空格需要增加2个ch…

Pytest精通指南(23)钩子函数-执行顺序(pytest-ordering)

文章目录 前言应用场景插件安装参数分析装饰方法装饰类装饰模块 前言 pytest-ordering 是一个pytest插件&#xff0c;它允许用户自定义测试用例的执行顺序。 默认情况下&#xff0c;pytest会按照模块、类、函数定义的顺序以及它们的名称的字母顺序来执行测试用例。 但通过使用 …

Kafka集群搭建可视化指南

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 Kafka集群搭建可视化指南 前言准备工作硬件要求环境准备 kafka集群的部署与配置3.1 单节点部署与多节点集群搭建单节点部署&#xff1a;多节点集群搭建&#xff1a; 3.2 Broker配置与优化3.3 Topic的创…

如何30天快速掌握键盘盲打

失业后在家备考公务员&#xff0c;发现了自己不正确的打字方式&#xff0c;决定每天抽出一点时间练习打字。在抖音上看到一些高手的飞速盲打键盘后&#xff0c;觉得使用正确的指法打字是很必要的。 练习打字&#xff0c;掌握正确的键盘指法十分关键。 练习打字的第一步是找到…

RIP最短路实验(思科)

华为设备参考&#xff1a;RIP最短路实验&#xff08;华为&#xff09; 一&#xff0c;技术简介 RIP&#xff08;Routing Information Protocol&#xff0c;路由信息协议&#xff09;是一种基于距离矢量的内部网关协议&#xff0c;工作原理是每个路由器周期性地向邻居路由器发…