參考文章:http://sishuok.com/forum/blogPost/list/2454.html#7084
SPRing DI:
1.Bean屬性:lazy-init(延遲初始化Bean): 延遲初始化也叫惰性初始化,并不提前初始化Bean,而是只有真正使用時才創(chuàng)建及初始化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("釋放資源,執(zhí)行一些清理操作..."); 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"); //一點要注冊銷毀回調,否則我們定義的銷毀的方法不執(zhí)行 context.registerShutdownHook(); DependBean dependBean=context.getBean("dependBean",DependBean.class); dependBean.write("AAAA"); }}執(zhí)行結果:Resource Bean------->初始化加載ResourceBean的配置文件內容DependBean-------------->初始化DependBean---------寫資源DependBean------------->銷毀Resource--------->銷毀釋放資源,執(zhí)行一些清理操作...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定義創(chuàng)建并皴法單例緩存池 prototype:即原型,每次向spring請求都返回全新的Bean,不緩存Bean,根據Bean定義創(chuàng)建全新的Bean 【web應用中作用域】 request:標示每個請求需要容器創(chuàng)建一個全新的Bean session:表示每個會話需要容器創(chuàng)建一個全新的Bean globalSession:類似于session作用域,只是擁有portlet環(huán)境的web應用,如果在非portlet環(huán)境將視為session作用域 自定義作用域:...
|
新聞熱點
疑難解答