当前位置:网站首页>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必须保持相同。
边栏推荐
- 接口设计时的一些建议
- 卷积神经网络在深度学习中新发展的5篇论文推荐
- BFC interview Brief
- Nmap scan
- LeetCode 7. 整数反转
- What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
- Ten years' experience of byte test engineer directly hits the pain point of UI automation test
- BFC面试简述
- word中使用自动插入题注功能
- Go notes (1) go language introduction and characteristics
猜你喜欢

仿ps样式js网页涂鸦板插件

6月“墨力原创作者计划”获奖名单公布!邀您共话国产数据库

idea配置标准注释

Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)

【服务器数据恢复】某品牌服务器存储raid5数据恢复案例

Pytorch---使用Pytorch实现LinkNet进行语义分割

Automatic generation of interface automatic test cases by actual operation

Leetcode+ 81 - 85 monotone stack topic

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

福昕PDF编辑器v10.1.8绿色版
随机推荐
奏响青春的乐章
word中插入图片后,图片上方有一空行,且删除后布局变乱
MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
Managed service network: application architecture evolution in the cloud native Era
Gobang go to work fishing tools can be LAN / man-machine
admas零件名重复
【申博攻略】六.如何联系心仪的博导
工厂从自动化到数字孪生,图扑能干什么?
D3.js+Three.js数据可视化3d地球js特效
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
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
企业数字化转型最佳实践案例:基于云的数字化平台系统安全措施简介与参考
WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
测试员的算法面试题-找众数
阿里测试师用UI自动化测试实现元素定位
Go language notes (4) go common management commands
测试用例 (TC)
jekins初始化密码没有或找不到
ACM组合计数入门
From automation to digital twins, what can Tupo do?