- 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]]