day38 05-Spring的BeanFactory与ApplicationContext区别
ApplicationContext怎么知道它是一个工厂呢?


BeanFactory也可以做刚才那些事情,只不过ApplicationContext对它有扩展。ApplicationContext间接继承BeanFactory。

ApplicationContext继承了很多接口,

其中接口HierarchicalBeanFactory继承了BeanFactory。所以说ApplicationContext继承了BeanFactory。

BeanFactory是延迟加载,使用到这个类才创建,不使用到这个类就不创建。ApplicationContext是只要一加载这个配置文件,就会创建这个类了。




package cn.itcast.spring3.demo1; import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource; public class SpringTest1 {
@Test
//传统方式
public void demo1(){
//造成程序紧密耦合.
//应该采用工厂+配置文件+反射的机制
HelloService helloService = new HelloServiceImpl();
helloService.sayHello();
}
@Test
//Spring开发
public void demo2(){
//创建一个Spring的工厂类.
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//ApplicationContext就是Spring的工厂类
//不写applicationContextx.xml的全路径,默认会去WEB-INF下面找applicationContextx.xml
//但是现在applicationContextx.xml写好之后已经发布到WEB-INF的classes里面
HelloService helloService = (HelloService) applicationContext.getBean("helloService");
helloService.sayHello();
}
@Test
//加载磁盘路径下的配置文件:
public void demo3(){
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("applicationContext.xml");
HelloService helloService = (HelloService) applicationContext.getBean("helloService");
helloService.sayHello();
}
@Test
public void demo4(){
// ClassPathResource FileSystemResource
//BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
@SuppressWarnings("deprecation")
BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("applicationContext.xml"));
HelloService helloService= (HelloService) beanFactory.getBean("helloService");
helloService.sayHello(); }
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- 别去schema,schema是文件,本地的文件,你得引那个头 --> <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>标签来设置类的信息,通过id属性为类起个标识. -->
<!-- 接口,实现类,配置文件也都有了 -->
<!-- 现在有一个工厂Spring为我们提供好了,其实就是解析这个XML文件 -->
<!-- 这个工厂你自己写会不会写?你用dom4j找里面的bean标签,找到class的属性值,然后就可以Class.forName()反射生成类的实例.其实Spring
也是这么做的,只不过工厂由Spring提供好了
-->
<bean id="helloService" class="cn.itcast.spring3.demo1.HelloServiceImpl">
<!-- 使用<property>标签注入属性
value指的是普通值
ref指的是对象
-->
<property name="info" value="传智播客(磁盘路径)"></property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<!-- 别去schema,schema是文件,本地的文件,你得引那个头 --> <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>标签来设置类的信息,通过id属性为类起个标识. -->
<!-- 接口,实现类,配置文件也都有了 -->
<!-- 现在有一个工厂Spring为我们提供好了,其实就是解析这个XML文件 -->
<!-- 这个工厂你自己写会不会写?你用dom4j找里面的bean标签,找到class的属性值,然后就可以Class.forName()反射生成类的实例.其实Spring
也是这么做的,只不过工厂由Spring提供好了
-->
<bean id="helloService" class="cn.itcast.spring3.demo1.HelloServiceImpl">
<!-- 使用<property>标签注入属性
value指的是普通值
ref指的是对象
-->
<property name="info" value="传智播客"></property>
</bean>
</beans>
day38 05-Spring的BeanFactory与ApplicationContext区别的更多相关文章
- Spring之BeanFactory与ApplicationConText区别
使用BeanFactory从xml配置文件加载bean: import org.springframework.beans.factory.xml.XmlBeanFactory; import org ...
- Spring中BeanFactory与ApplicationContext的区别
BeanFactory:Bean工厂接口,是访问Spring Bean容器的根接口,基本Bean视图客户端.从其名称上即可看出其功能,即实现Spring Bean容器的读取. ApplicationC ...
- Spring加载xml配置文件的方式(BeanFactory和ApplicationContext区别)
描述 大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件 ...
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...
- spring:Beanfactory和ApplicationContext、BeanFactory 和 FactoryBean
1.Beanfactory和ApplicationContext有什么区别 ApplicationContext (1)在配置文件加载后创建bean 利用debug方式,在Student类的无参构造方 ...
- Spring中BeanFactory和ApplicationContext的区别
1. BeanFactory负责读取bean配置文档,管理bean的加载,实例化,维护bean之间的依赖关系,负责bean的生命周期. 2. ApplicationContext除了提供上述BeanF ...
- BeanFactory 和 ApplicationContext 区别
区别 BeanFactory: Spring里面最低层的接口,提供了最简单的容器的功能,只提供了实例化对象和拿对象的功能 BeanFactory在启动的时候不会去实例化Bean,中有从容器中拿Bean ...
- Spring之BeanFactory与ApplicationConText
:BeanFactory基本的工厂解析,管理,实例化所有容器内的bean的接口,spring中所有解析配置文件的类都直接或者间接实现该接口ApplicationContext接口implements ...
- BeanFactory和ApplicationContext的区别(Bean工厂和应用上下文)
https://blog.csdn.net/qq_20757489/article/details/88543252 https://blog.csdn.net/pythias_/article/de ...
随机推荐
- 莫烦PyTorch学习笔记(六)——批处理
1.要点 Torch 中提供了一种帮你整理你的数据结构的好东西, 叫做 DataLoader, 我们能用它来包装自己的数据, 进行批训练. 而且批训练可以有很多种途径. 2.DataLoader Da ...
- LintCode_1 单例模式
从今天开始我的LintCode之旅,由于C/C++好久没有使用了,语法生疏不说,低级错误频繁出现,因此在做题之后,还会有部分时间复习语法项目. ---------------------------- ...
- 34 N皇后问题Ⅱ
原题网址:https://www.lintcode.com/zh-cn/old/problem/n-queens-ii/ 34. N皇后问题 II 描述 笔记 数据 评测 讨论区 根据n皇后问题, ...
- Python网络爬虫之三种数据解析方式 (xpath, 正则, bs4)
引入 回顾requests实现数据爬取的流程 指定url 基于requests模块发起请求 获取响应对象中的数据 进行持久化存储 其实,在上述流程中还需要较为重要的一步,就是在持久化存储之前需要进行指 ...
- 网络爬虫技术Jsoup
Jsoup介绍:Jsoup 是一个 Java 的开源HTML解析器,可直接解析某个URL地址.HTML文本内容 Jsoup主要有以下功能: 1. 从一个URL,文件或字符串中解析HTML 2. 使用D ...
- Hbase实验:java创建和删除table
开启zookeeper.hadoop.hbase: 打开eclipse创一个java project,然后导入所需jar包: 写好java代码,运行create,然后去hbase shell里查看: ...
- SolrCloud4.7.1分布式部署
一.环境 软件: apache-tomcat-7.0.53.tar.gz solr-4.7.1.tgz zookeeper-3.4.6.tar.gz 规划: 三个节点IP: 192.168.50. ...
- Vue+jquery上拉加载
<ul> <li class="new-list" v-for="item in proarr"> <a :href=" ...
- 13 个最佳 JavaScript 数据网格库
13 个最佳 JavaScript 数据网格库 转自:开源中国 www.oschina.net/translate/best-javascript-data-grid-libraries Java ...
- PAT甲级——A1041 Be Unique
Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...