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

server/2024/9/24 11:26:22/

目录

 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不知道应该选择哪一个来注入

java">// 一个接口  
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

 

java"> 
@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 方法  }  
}

多例模式

java">//这边是多实例
@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.ppmy.cn/server/6994.html

相关文章

OpenAI、蚂蚁集团、谷歌、科大讯飞等联合编制大模型安全国际标准,已正式发布

4月15日-19日,第27届联合国科技大会在瑞士日内瓦召开。16日,在以“塑造AI的未来”为主题的AI边会上,世界数字技术院(WDTA)发布了一系列突破性成果,包括《生成式人工智能应用安全测试标准》和《大语言模型安…

4月21敲一篇猜数字游戏,封装函数,void,无限循环,快去体验体验

今天敲一篇猜数字游戏 目录 今天敲一篇猜数字游戏 1.打开先学goto语句: 2.开干: 首次我们学习随机数: 讲解一下: 改用srand; 加入时间变量: 获取时间:哈​编辑 3.我本来想已近够完美了&#xff0…

Spring MVC 中的适配器模式

文章目录 Spring MVC 中的适配器模式为什么不直接调用?解决方案一:统一 Controller解决方案二:使用适配器模式DispatcherServlet 对 Adpater 的使用 Spring MVC 中的适配器模式 为什么不直接调用? DispatcherServlet 为什么不直…

appium控制手机一直从下往上滑动

用于使用Appium和Selenium WebDriver在Android设备上滚动设置应用程序的界面。具体来说,它通过WebDriverWait和expected_conditions等待元素出现,然后使用ActionChains移动到该元素并执行滚动动作。在setUp中,它初始化了Appium的WebDriver和c…

redis7安装与配置

一、下载 通过 redis官网 或者 redis中文网 下载。 以下是 redis 相关文档资料链接: redis源码地址 redis在线测试 redis命令参考 redis中文文档 历史发布版本的源码地址 二、版本命名规则 Redis从发布到现在,已经有十余年的时光了,…

OpenHarmony轻量系统开发【7】驱动之I2C显示OLED屏幕

7.1实验效果 Hispark WiFi开发套件又提供一个oled屏幕,但是鸿蒙源码中没有这个屏幕的驱动,我们需要自己去移植。 以下是移植效果: 接口:I2C 使用引脚:HI_IO_NAME_GPIO_13 、 HI_IO_NAME_GPIO_14 7.2代码 这里我直…

网站怎么实现HTTPS访问?

网站实现HTTPS的过程主要分为以下几个步骤: 1. 申请SSL证书: - 根据网站需求选择合适的SSL证书类型,DV证书只需验证域名所有权,适用于个人网站或小型项目;OV和EV证书需验证企业身份信息,适用于对信任度要求…

行业模板|DataEase批发零售大屏模板推荐

DataEase开源数据可视化分析平台于2022年6月发布模板市场(https://templates-de.fit2cloud.com),并于2024年1月新增适用于DataEase v2版本的模板分类。模板市场旨在为DataEase用户提供专业、美观、拿来即用的大屏模板,方便用户根据…