BeanFactory 和 ApplicationContext 区别
区别
BeanFactory:
Spring里面最低层的接口,提供了最简单的容器的功能,只提供了实例化对象和拿对象的功能
BeanFactory在启动的时候不会去实例化Bean,中有从容器中拿Bean的时候才会去实例化
ApplicationContext:
应用上下文,继承BeanFactory接口,它是Spring的一各更高级的容器,提供了更多的有用的功能
1) 国际化(MessageSource)
2) 访问资源,如URL和文件(ResourceLoader)
3) 载入多个(有继承关系)上下文 ,使得每一个上下文都专注于一个特定的层次,比如应用的web层
4) 消息发送、响应机制(ApplicationEventPublisher)
5) AOP(拦截器)
ApplicationContext在启动的时候就把所有的非延迟加载Bean全部实例化了。它还可以为Bean配置 lazy-init=true 来让Bean延迟实例化
国际化(MessageSource)
<!-- 资源国际化 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>properties/messages</value> <!-- proerties包下的以messages为主要名称的properties文件 -->
</list>
</property>
</bean>
Spring访问资源(ResourceLoader)
ApplicationContext acxt =new ClassPathXmlApplicationContext("/applicationContext.xml");
1.通过虚拟路径来存取:当资源位于CLASSPATH路径下时,可以采用这种方式来存取。
Resource resource = acxt.getResource("classpath:messages_en_CN.properties");
2.通过绝对路径存取资源文件。
Resource resource = acxt.getResource("file:F:/messages_en_CN.properties");
3.相对路径读取资源文件。
Resource resource = acxt.getResource("/messages_en_CN.properties");
Spring载入多个上下文
方法一:
<import resource="applicationContext.xml"/>
方法二:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext-security.xml,applicationContext-dao.xml,applicationContext-Service.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Spring的AOP(常用的是拦截器)
一般拦截器都是实现HandlerInterceptor,其中有三个方法preHandle、postHandle、afterCompletion
1. preHandle:执行controller之前执行
2. postHandle:执行完controller,return modelAndView之前执行,主要操作modelAndView的值
3. afterCompletion:controller返回后执行
配置:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/login"/>
<mvc:exclude-mapping path="/logout"/>
<ref bean="interceptor" />
</mvc:interceptor>
</mvc:interceptors>
public class Interceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler)
throws Exception {
return true;
}
}
BeanFactory 和 ApplicationContext 区别的更多相关文章
- Spring加载xml配置文件的方式(BeanFactory和ApplicationContext区别)
描述 大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件 ...
- Spring之BeanFactory与ApplicationConText区别
使用BeanFactory从xml配置文件加载bean: import org.springframework.beans.factory.xml.XmlBeanFactory; import org ...
- day38 05-Spring的BeanFactory与ApplicationContext区别
ApplicationContext怎么知道它是一个工厂呢? BeanFactory也可以做刚才那些事情,只不过ApplicationContext对它有扩展.ApplicationContext间接 ...
- BeanFactory和ApplicationContext的作用和区别
BeanFactory和ApplicationContext的作用和区别 作用: 1. BeanFactory负责读取bean配置文档,管理bean的加载,实例化,维护bean之间的依赖关系,负责be ...
- spring中的BeanFactory与ApplicationContext的作用和区别?
BeanFactory类关系继承图 1. BeanFactory类结构体系: BeanFactory接口及其子类定义了Spring IoC容器体系结构,由于BeanFactory体系非常的庞大和复杂, ...
- Spring中BeanFactory与ApplicationContext的区别
BeanFactory:Bean工厂接口,是访问Spring Bean容器的根接口,基本Bean视图客户端.从其名称上即可看出其功能,即实现Spring Bean容器的读取. ApplicationC ...
- BeanFactory 和 ApplicationContext的区别
今天在网上查资料无意中看到这一行代码 BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext ...
- Spring中的BeanFactory和ApplicationContext的区别
我用一个例子去测试BeanFactory和ApplicationContext的区别 首先建立一个bean public class User { //声明无参构造,打印一句话,监测对象创建时机 pu ...
- BeanFactory和ApplicationContext的区别+部分Spring的使用
BeanFactory和ApplicationContext的区别 ApplicationContext 方式加载:创建容器的同时 容器初始化,容器所有的bean创建完毕 Spring容器中获取一 ...
随机推荐
- MYSQL-连续出现的数字
编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+| Id | Num |+----+-----+| 1 | 1 || 2 | 1 || 3 | 1 || 4 | 2 ...
- hdu 3746 kmp的next数组理解
题目大意: 求最少在结尾补上几个字符才能形成循环 基本思路: next数组有一个性质,长度为len的字符串的最小长度的循环节(可能没有,但有的话一定是)len-next[len],因为最长不能是原串, ...
- oscache缓存
oscache 使用总结 Posted on 2009-05-22 22:45 青果 阅读(1270) 评论(2) 编辑 收藏 所属分类: 技术点滴 前阵子对公司网站进行了性能优化,其中,有一项 ...
- Java中static关键字的定义
1.static存在的主要意义 static的主要意义是在于创建独立于具体对象的域变量或者方法.以致于即使没有创建对象,也能使用属性和调用方法! static关键字还有一个比较关键的作用就是 用来形成 ...
- hbase-2.0.4集群部署
hbase-2.0.4集群部署 1. 集群节点规划: rzx1 HMaster,HRegionServer rzx2 HRegionServer rzx3 HRegionServer 前提:搭建好ha ...
- go声明和初始化
go声明和初始化 当我们第一次看见变量和声明时,我们仅仅看见一些内置的类型,比如整型和字符串.现在我们将学习结构体,并且我们会深入学习包括指针的内容. 通过一种最简单的方式去创建一个结构体值类型: g ...
- Sqlserver复杂查询
--联表修改 update xyzrb set xyzrb.xy_card=tablsb.card from xyzrb left join tablsb on xyzrb.xybh=tablsb.x ...
- 【Codeforces Round #429 (Div. 2) C】Leha and Function
[Link]:http://codeforces.com/contest/841/problem/C [Description] [Solution] 看到最大的和最小的对应,第二大的和第二小的对应. ...
- C语言新手写扫雷源代码
今天发布源代码,由于写在一个文件里非常乱,所以分三个文件写 绘图和鼠标函数graph.h /*绘图与鼠标相关函数*/ #include<graphics.h> #include <e ...
- AndroidFine Error:Annotation processors must be explicitly declared now.
环境 Android Studio 3.0 Gradle 3.0.0 gradle 4.1 Error Error:Execution failed for task ':app:javaPreCom ...