参考文章:http://sishuok.com/forum/blogPost/list/2454.html#7084
SPRing DI:
1.Bean属性:lazy-init(延迟初始化Bean): 延迟初始化也叫惰性初始化,并不提前初始化Bean,而是只有真正使用时才创建及初始化Bean 容器每个Bean只有一个实例 配置文件:lazy-init="true" 默认为false ioc容器进行初始化的时候就初始化Bean2.depends-on:【只能是singleton作用销毁,prototype作用域不能】 具有此属性指定的Bean要先初始化完毕之后才初始化当前Bean 资源初始化与释放的问题: <bean id="A" class="XXX"> <bean id="B" class="xxx" depends-on="A,C,...."> 初始化与销毁: 加载B类,先需要准备其依赖的A,C类资源,先加载A,C类的资源这些前奏的工作,然后再进行加载B类资源 销毁A,B,C类资源,先需要销毁B类资源,因为销毁A,C类资源,B类资源有可能会对A,C类资源的访问,会 造成资源的不释放与释放错误。故先销毁B类资源,然后再销毁A,C类资源以下是depends-on代码例子:
被引用的类:package spring.depend;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class ResourceBean { private FileOutputStream fos; private File file; //初始化方法 public void init(){ System.out.println("Resource Bean------->初始化"); //加载资源 System.out.println("加载ResourceBean的配置文件内容"); try { this.fos=new FileOutputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } } //销毁资源方法 public void destory(){ System.out.println("Resource--------->销毁"); System.out.println("释放资源,执行一些清理操作..."); try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } public FileOutputStream getFos() { return fos; } public void setFile(File file) { this.file = file; } }引用的类:package spring.depend;import java.io.IOException;public class DependBean { ResourceBean resourceBean; public ResourceBean getResourceBean() { return resourceBean; } public void setResourceBean(ResourceBean resourceBean) { this.resourceBean = resourceBean; } public void write(String name){ System.out.println("DependBean---------写资源"); try { resourceBean.getFos().write(name.getBytes()); } catch (IOException e) { e.printStackTrace(); } } //初始化方法 public void init(){ System.out.println("DependBean-------------->初始化"); try { resourceBean.getFos().write("DependBean--------->初始化".getBytes()); } catch (IOException e) { e.printStackTrace(); } } //销毁方法 public void destory(){ System.out.println("DependBean------------->销毁"); //在销毁之前需要往文件中写销毁内容 try { resourceBean.getFos().write("DependBean--------->销毁".getBytes()); } catch (IOException e) { e.printStackTrace(); } }}xml:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 被引用类的初始化 --> <bean id="resourceBean" class="spring.depend.ResourceBean" init-method="init" destroy-method="destory"> <property name="file" value="E:/ykd2.sql"/> </bean> <!-- 引用类的初始化 --> <bean id="dependBean" class="spring.depend.DependBean" init-method="init" destroy-method="destory" depends-on="resourceBean"> <property name="resourceBean" ref="resourceBean"></property> </bean> </beans> Test类:package spring;import org.junit.Test;import org.springframework.context.support.ClassPathXmlapplicationContext;import spring.depend.DependBean;public class DependOnTest { @Test public void testDependOn(){ ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("depend-on.xml"); //一点要注册销毁回调,否则我们定义的销毁的方法不执行 context.registerShutdownHook(); DependBean dependBean=context.getBean("dependBean",DependBean.class); dependBean.write("AAAA"); }}执行结果:Resource Bean------->初始化加载ResourceBean的配置文件内容DependBean-------------->初始化DependBean---------写资源DependBean------------->销毁Resource--------->销毁释放资源,执行一些清理操作...3.自动装配: 由sprng来自动注入依赖对象,无需人工参与,减少构造器注入和setter注入配置[我们之前写的class有对应的属性的时候,需要进行set方法的设定] 通过bean标签中的autowirte属性来改变自动装配方式,属性值如下: 1.default:默认装配 no,byName,byType,constructor no:不支持自动装配 byName:根据名字来自动装配,只能用于setter注入 byType:根据类型注入,用于setter注入 4.dependcy-check: 依赖检查 none:默认方式,表示不检查 objects:检查除基本类型外的依赖对象 simple:对基本类型依赖检查 all:对所有类型进行依赖检查 5.scope:[spring的作用域] single:在springioc容器存在一个实例,而且完整生命周期完全由spring管理 spring缓存单例对象,Bean定义也是会缓存的,对于惰性初始化对象在首次使用时根据Bean定义创建并皴法单例缓存池 prototype:即原型,每次向spring请求都返回全新的Bean,不缓存Bean,根据Bean定义创建全新的Bean 【web应用中作用域】 request:标示每个请求需要容器创建一个全新的Bean session:表示每个会话需要容器创建一个全新的Bean globalSession:类似于session作用域,只是拥有portlet环境的web应用,如果在非portlet环境将视为session作用域 自定义作用域:...
新闻热点
疑难解答