当前位置:网站首页>ApplicationContext 与 BeanFactory 区别(MS)
ApplicationContext 与 BeanFactory 区别(MS)
2022-07-04 20:09:00 【Youcan.】
1. 从继承关系上来说,BeanFactory 是 的父类。BeanFactory 只是提供了基础操作Bean的方法,ApplicationContext除了拥有父类的基础操作之外,还提供了自己独有的功能。
2. 从性能方面上来说,ApplicationContext 是一次性加载并初始化所有的Bean对象,但是BeanFactory 是需要哪个Bean才去加载哪个Bean。
ApplicationContext 是在创建的时候就会将所有的bean初始化,第一次使用bean的时候快,启动比较慢,但是可能会造成资源浪费。BeanFactory不浪费任何资源,第一次加载bean比较慢。
import beans.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @program: first-spring
* @description: 通过ApplicationContext来获取对象
* @author: DRY
* @create: 2022-07-03 12:47
**/
public class App {
public static void main(String[] args) {
//1. 先得到上下文对象
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
//2. 从spring上下文对象中获取bean
User user = (User)context.getBean("user");
//3. 使用对象(可以不使用)
user.sayHi("张丹");
}
}
import beans.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
/**
* @program: first-spring
* @description: 通过BeanFactory 来获取spring上下文对象
* @author: DRY
* @create: 2022-07-03 13:06
**/
public class App2 {
public static void main(String[] args) {
//1.先得到BeanFactory来获取对象
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
//2. 获取bean
User user = (User) beanFactory.getBean("user");
//3. 使用bean
user.sayHi("李四");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 表示将一个bean对象存储到spring中(可以有多个bean标签)-->
<bean id="user" class="beans.User"></bean>
</beans>

这两个user必须保持相同。
边栏推荐
- 测试用例 (TC)
- Après l'insertion de l'image dans le mot, il y a une ligne vide au - dessus de l'image, et la disposition est désordonnée après la suppression
- Hwinfo hardware detection tool v7.26 green version
- Advantages of RFID warehouse management system solution
- How does win11 search for wireless displays? Win11 method of finding wireless display device
- Ten years' experience of byte test engineer directly hits the pain point of UI automation test
- Automatic insertion of captions in word
- Qt五子棋人机对战画棋子之QPainter的使用误区总结
- Actual combat simulation │ JWT login authentication
- PermissionError: [Errno 13] Permission denied: ‘data.csv‘
猜你喜欢

NetWare r7000 Merlin system virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements. Solution, is it necessary to create virtual memory??

Jmeter 之压测入门

分析伦敦银走势图的技巧

【optimtool.unconstrain】无约束优化工具箱

MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询

五子棋 上班摸鱼工具 可局域网/人机

【1200. 最小絕對差】

HWiNFO硬件检测工具v7.26绿色版

《动手学深度学习》(三) -- 卷积神经网络 CNN

In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
随机推荐
D3.js+Three.js数据可视化3d地球js特效
PS竖排英文和数字文字怎么改变方向(变竖直显示)
Foxit pdf editor v10.1.8 green version
[solution] paddlepaddle 2 X call static graph mode
Android原生数据库的基本使用和升级
Hands on deep learning (III) -- convolutional neural network CNN
LeetCode 7. Integer inversion
WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
word中插入圖片後,圖片上方有一空行,且删除後布局變亂
Browser render page pass
Advantages of RFID warehouse management system solution
colResizable.js自动调整表格宽度插件
RFID仓库管理系统解决方案有哪些功能模块
idea大小写快捷键
哈希表、哈希函数、布隆过滤器、一致性哈希
伦敦银走势图分析的新方法
搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
What are the functional modules of RFID warehouse management system solution
Record the online bug solving list (unfinished to be continued 7/4)
扩展你的KUBECTL功能