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

相关文章

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@