当前位置:网站首页>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 测试用例
- MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
- How does the computer save web pages to the desktop for use
- 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
- 《动手学深度学习》(三) -- 卷积神经网络 CNN
- 语义化标签的优势和块级行内元素
- What if the brightness of win11 is locked? Solution to win11 brightness locking
- go defer的使用说明
- 五子棋 上班摸鱼工具 可局域网/人机
- Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
猜你喜欢

Hwinfo hardware detection tool v7.26 green version

Quelques suggestions pour la conception de l'interface
![[observation] Lenovo: 3x (1+n) smart office solution, releasing the](/img/e4/f660461c42eb81ab8c94fd87503a83.png)
[observation] Lenovo: 3x (1+n) smart office solution, releasing the "multiplier effect" of office productivity

Alibaba testers use UI automated testing to achieve element positioning

render函数与虚拟dom

js 3D爆炸碎片图片切换js特效

测试员的算法面试题-找众数
![[1200. Minimum absolute difference]](/img/fa/4ffbedd8f24c75a20d3eaeaf0430ae.png)
[1200. Minimum absolute difference]

二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】

c语言函数形参自增自减情况分析
随机推荐
Sword finger offer II 80-100 (continuous update)
From automation to digital twins, what can Tupo do?
idea配置标准注释
MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
Go language notes (2) some simple applications of go
go笔记(1)go语言介绍以及特点
黄金k线图中的三角形有几种?
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
[Shenbo introduction] VI How to contact your favorite doctoral tutor
Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
word中插入图片后,图片上方有一空行,且删除后布局变乱
hash 表的概念及应用
idea恢复默认快捷键
网络命名空间
How does wincc7.5 SP1 find variables and their positions through cross indexing?
How to solve the problem that win11 cannot write the value to the registry key?
看腾讯大老如何做接口自动化测试
Advantages of semantic tags and block level inline elements
c语言函数形参自增自减情况分析