spring 04-Spring框架依賴注入基本使用
阿新 • • 發佈:2018-11-29
- Spring的依賴注入形式實際上所有物件產生控制都要通過applicationContext.xml檔案實現
- 依賴注入必須啟動容器後才可以進行該配置檔案的內部的載入操作
依賴注入之有參構造
定義一個Dept類
package cn.liang.vo; import java.util.List; public class Dept { private Integer deptno ; private String dname ; private String loc ; public Dept(Integer deptno, String dname, String loc) { super(); this.deptno = deptno; this.dname = dname; this.loc = loc; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } public String getLoc() { return loc; } public void setLoc(String loc) { this.loc = loc; } @Override public String toString() { return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]"; } }
修改applicationContext.xml檔案,進行Dept類物件的定義
<bean id="dept" class="cn.liang.vo.Dept"> <constructor-arg index="0" value="10"/> <constructor-arg index="1" value="Development department"/> <constructor-arg index="2" value="Guangzhou"/> </bean>
測試程式
- 通過id(dept)取得Dept類物件時,自動呼叫構造方法,並將以上引數進行傳遞控制
package cn.liang.test; import org.apache.log4j.Logger; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.liang.vo.Dept; import junit.framework.TestCase; public class TestMessageService2 { private static ApplicationContext ctx = null ; static { ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testDeptConstructor() { Dept dept = (Dept) ctx.getBean("dept",Dept.class) ; Logger.getLogger(TestMessageService.class).info(dept); } }
輸出結果
2018-11-29 16:09:19,557 INFO [cn.liang.test.TestMessageService] - Dept [deptno=10, dname=Development department, loc=Guangzhou]
依賴注入之無參構造
定義一個Dept類
package cn.liang.vo; import java.util.List; public class Dept { private Integer deptno ; private String dname ; private String loc ; public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } public String getLoc() { return loc; } public void setLoc(String loc) { this.loc = loc; } @Override public String toString() { return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]"; } }
修改applicationContext.xml檔案,進行Dept類物件的定義
- property呼叫的是類中的屬性
- 當明確指定了value屬性設定內容,容器會自動呼叫setter方法
<bean id="dept" class="cn.liang.vo.Dept"> <property name="deptno" value="10"/> <property name="dname" value="Development department"/> <property name="loc" value="Guangzhou"/> </bean>
測試程式testDeptConstructor,輸出結果
2018-11-29 16:10:32,128 INFO [cn.liang.test.TestMessageService] - Dept [deptno=10, dname=Development department, loc=Guangzhou]
依賴注入之引用操作
定義一個Emp類
package cn.liang.vo; public class Emp { private Integer empno ; private String ename ; private Double sal ; private Dept dept ; public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } @Override public String toString() { return "Emp [empno=" + empno + ", ename=" + ename + ", sal=" + sal + ", dept=" + dept + "]"; } }
修改applicationContext.xml檔案,進行Dept類和Emp類物件的定義
- 本次操作通過Emp找到Dept的關係,使用屬性ref
<bean id="emp" class="cn.liang.vo.Emp"> <property name="empno" value="1234"/> <property name="ename" value="Liang"/> <property name="sal" value="18000.00"/> <property name="dept" ref="dept"/> </bean> <bean id="dept" class="cn.liang.vo.Dept"> <property name="deptno" value="10"/> <property name="dname" value="Development department"/> <property name="loc" value="Guangzhou"/> </bean>
測試程式
package cn.liang.test; import org.apache.log4j.Logger; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.liang.vo.Dept; import cn.liang.vo.Emp; import junit.framework.TestCase; public class TestMessageService2 { private static ApplicationContext ctx = null ; static { // 靜態程式碼塊優先於所有的程式碼執行,目的是為了靜態屬性初始化 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testEmpConstructor() { Emp emp = (Emp) ctx.getBean("emp",Emp.class) ; Logger.getLogger(TestMessageService.class).info(emp); } }
輸出結果
2018-11-29 16:23:05,418 INFO [cn.liang.test.TestMessageService] - Emp [empno=1234, ename=Liang, sal=18000.0, dept=Dept [deptno=10, dname=Development department, loc=Guangzhou]]
依賴注入之p名稱空間操作
- p名稱空間的核心本質是能通過一行配置就可以解決多行問題
主要核心配置
xmlns:p="http://www.springframework.org/schema/p"
主要語法
<bean id="dept" class="cn.liang.vo.Dept" p:deptno="10" p:dname="Development department" p:loc="Guangzhou"/> <bean id="emp" class="cn.liang.vo.Emp" p:empno="1234" p:ename="Liang" p:sal="18000.00" p:dept-ref="dept"/>
測試程式testEmpConstructor,輸出結果
2018-11-29 16:28:41,085 INFO [cn.liang.test.TestMessageService] - Emp [empno=1234, ename=Liang, sal=18000.0, dept=Dept [deptno=10, dname=Development department, loc=Guangzhou]]