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

2024-04-18 14:20

本文主要是介绍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实例的地址是相同的

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

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

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

这篇关于java-Spring-入门学习-第二天(单例模式和多例模式)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/915028

相关文章

使用SpringBoot+InfluxDB实现高效数据存储与查询

《使用SpringBoot+InfluxDB实现高效数据存储与查询》InfluxDB是一个开源的时间序列数据库,特别适合处理带有时间戳的监控数据、指标数据等,下面详细介绍如何在SpringBoot项目... 目录1、项目介绍2、 InfluxDB 介绍3、Spring Boot 配置 InfluxDB4、I

基于Java和FFmpeg实现视频压缩和剪辑功能

《基于Java和FFmpeg实现视频压缩和剪辑功能》在视频处理开发中,压缩和剪辑是常见的需求,本文将介绍如何使用Java结合FFmpeg实现视频压缩和剪辑功能,同时去除数据库操作,仅专注于视频处理,需... 目录引言1. 环境准备1.1 项目依赖1.2 安装 FFmpeg2. 视频压缩功能实现2.1 主要功

使用Java读取本地文件并转换为MultipartFile对象的方法

《使用Java读取本地文件并转换为MultipartFile对象的方法》在许多JavaWeb应用中,我们经常会遇到将本地文件上传至服务器或其他系统的需求,在这种场景下,MultipartFile对象非... 目录1. 基本需求2. 自定义 MultipartFile 类3. 实现代码4. 代码解析5. 自定

Spring-DI依赖注入全过程

《Spring-DI依赖注入全过程》SpringDI是核心特性,通过容器管理依赖注入,降低耦合度,实现方式包括组件扫描、构造器/设值/字段注入、自动装配及作用域配置,支持灵活的依赖管理与生命周期控制,... 目录1. 什么是Spring DI?2.Spring如何做的DI3.总结1. 什么是Spring D

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

spring AMQP代码生成rabbitmq的exchange and queue教程

《springAMQP代码生成rabbitmq的exchangeandqueue教程》使用SpringAMQP代码直接创建RabbitMQexchange和queue,并确保绑定关系自动成立,简... 目录spring AMQP代码生成rabbitmq的exchange and 编程queue执行结果总结s

Java调用Python脚本实现HelloWorld的示例详解

《Java调用Python脚本实现HelloWorld的示例详解》作为程序员,我们经常会遇到需要在Java项目中调用Python脚本的场景,下面我们来看看如何从基础到进阶,一步步实现Java与Pyth... 目录一、环境准备二、基础调用:使用 Runtime.exec()2.1 实现步骤2.2 代码解析三、

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

Spring Boot 整合 SSE(Server-Sent Events)实战案例(全网最全)

《SpringBoot整合SSE(Server-SentEvents)实战案例(全网最全)》本文通过实战案例讲解SpringBoot整合SSE技术,涵盖实现原理、代码配置、异常处理及前端交互,... 目录Spring Boot 整合 SSE(Server-Sent Events)1、简述SSE与其他技术的对