#spring核心技术
## 基本原理-容器和bean
1、在spring中,那些组成你应用程序的主体(backbone)及由spring ioc容器所管理的对象,被称之为bean。 简单地讲,bean就是由spring容器初始化、装配及管理的对象。
2、org.springframework.beans.factory.beanfactory 是spring ioc容器的实际代表者,ioc容器负责容纳此前所描述的bean,并对bean进行管理。
3、在spring中,beanfactory是ioc容器的核心接口。 它的职责包括:实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。
4、applicationcontext在web应用中的实例化
contextconfiglocation /web-inf/daocontext.xml /web-inf/applicationcontext.xml org.springframework.web.context.contextloaderlistener
contextloaderlistener首先检查contextconfiglocation参数,如果它不存在,它将使用/web-inf/applicationcontext.xml作为默认值。如果已存在,它将使用分隔符(逗号、冒号或空格)将字符串分解成应用上下文件位置路径。
5、实例化容器
applicationcontext context = new classpathxmlapplicationcontext( new string[] {"services.xml", "daos.xml"});
## 依赖
1、依赖注入(di)背后的基本原理是对象之间的依赖关系(即一起工作的其它对象)只会通过以下几种方式来实现:setter注入和构造器注入。因此,容器的工作就是创建bean时注入那些依赖关系。
2、循环依赖
(1)构造器的循环依赖
(2)field属性的循环依赖
构造器的循环依赖问题无法解决,只能拋出beancurrentlyincreationexception异常,在解决属性循环依赖时,spring采用的是提前暴露对象的方法。
spring的循环依赖的理论依据基于java的引用传递,当获得对象的引用时,对象的属性是可以延后设置的(但是构造器必须是在获取引用之前)。
3、static工厂方法返回对象实例
public class examplebean { // a private constructor private examplebean(...) { ... } public static examplebean createinstance ( anotherbean anotherbean, int i) { examplebean eb = new examplebean (...); // some other operations... return eb; } }
请注意,传给static工厂方法的参数由constructor-arg元素提供,这与使用构造器注入时完全一样。
## bean的作用域
singleton:在每个spring ioc容器中一个bean定义对应一个对象实例。
prototype:一个bean定义对应多个对象实例。
request:在一次http请求中,一个bean定义对应一个实例;即每次http请求将会有各自的bean实例, 它们依据某个bean定义创建而成。该作用域仅在基于web的spring applicationcontext情形下有效。
session:在一个http session中,一个bean定义对应一个实例。该作用域仅在基于web的spring applicationcontext情形下有效。
global session:在一个全局的http session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的spring applicationcontext情形下有效。
1、根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。
2、request、session以及global session 仅在基于web的应用中使用,如果你用spring web mvc,即用springdispatcherservlet或dispatcherportlet来处理请求,则不需要做特别的配置:dispatcherservlet 和 dispatcherportlet已经处理了所有有关的状态,否则需要在web应用的'web.xml'文件中增加 javax.servlet.servletrequestlistener 定义
org.springframework.web.context.request.requestcontextlistener
## 定制bean特性
### 生命周期回调
1、spring提供了几个标志接口(marker interface),这些接口用来改变容器中bean的行为;它们包括initializingbean
和disposablebean。实现这两个接口的bean在初始化和析构时容器会调用前者的afterpropertiesset()方法,
以及后者的destroy()方法。
2、spring在内部使用beanpostprocessor实现来处理它能找到的任何标志接口并调用相应的方法。
如果你需要自定义特性或者生命周期行为,你可以实现自己的 beanpostprocessor。
### 初始化回调
1、实现org.springframework.beans.factory.initializingbean接口允许容器在设置好bean的所有必要属性后,执行初始化事宜。
initializingbean接口仅指定了一个方法
void afterpropertiesset() throws exception;
2、通常,要避免使用initializingbean接口并且不鼓励使用该接口,因为这样会将代码和spring耦合起来。
有一个可选的方案是,可以在bean定义中指定一个普通的初始化方法,然后在xml配置文件中通过指定
init-method属性来完成。
如下面的定义所示:
public class examplebean { public void init() { // do some initialization work } }
### 析构回调
1、实现org.springframework.beans.factory.disposablebean接口的bean允许在容器销毁该bean的时候获得
一次回调。disposablebean接口也只规定了一个方法:
void destroy() throws exception;
2、通常,要避免使用disposablebean标志接口而且不鼓励使用该接口,因为这样会将代码与spring耦合在一
起。有一个可选的方案是,在bean定义中指定一个普通的析构方法,然后在xml配置文件中通过指定
destroy-method属性来完成。如下面的定义所示:
public class examplebean { public void cleanup() { // do some destruction work (like releasing pooled connections) } }
3、在spring中有三种方式可以控制bean的生命周期行为: initializingbean 和 disposablebean 回调接口;
自定义init() 和 destroy() 方法; @postconstruct 和@predestroy annotations.
4、在非web应用中优雅地关闭spring ioc容器
abstractapplicationcontext ctx = new classpathxmlapplicationcontext(new string []{"beans.xml"}); // add a shutdown hook for the above context... ctx.registershutdownhook();
相关推荐
spring核心技术源代码spring核心技术源代码spring核心技术源代码spring核心技术源代码
spring核心技术学习总结 spring核心技术学习总结
spring核心技术文档,学习spring的必备资料
spring核心技术文档. spring核心技术文档.
spring 核心技术 最佳实践 -->有兴趣可以看看
本文介绍了spring的核心技术及原理以及spring的配置
spring 2.0 有基础到深入详细讲解教程,适合初学者到高级工程师使用
02_spring核心技术-ioc.ppt
springcloud核心技术-初识springcloud微服务凯发k8国际娱乐官网入口的解决方案.docx
ioc容器 bean的基本配置 以及其他一些简单配置
详细讲解spring..精通spring核心技术..
本人springboot学习笔记总结,根据视频初学,感兴趣可下载。
本参考文档涵盖了spring框架中绝对不可或缺的所有技术。 其中最重要的是spring框架的控制反转(inversion of control,ioc)容器(container)。在对spring框架的ioc容器进行全面介绍之后,将全面介绍spring的面向切...
《搞定j2ee核心技术与企业应用:ajax,jsp,struts 2,spring,hibernate》是笔者在多年项目开发过程中的经验总结,它通过丰富的实例由浅入深、循序渐进地介绍了目前采用java进行web开发的各种框架的使用方法,从而...
spring技术内幕:深入解析spring架构与设计原理.pdf
spring boot核心技术-笔记
spring核心技术(高清视频教程) .rar
尚硅谷spring boot 视频,包含上、下,核心技术和整合!最新出炉,有需求的可以下载看看,有整合!
spring boot 2.0深度实践之核心技术篇 学习视频教程中的源码