DI:依赖注入
第一个DEMO:域属性注入
java类:(Car类和Stu类,学生有一辆小汽车)
1 package cn.dawn.day02di; 2 /** 3 * Created by Dawn on 2018/3/3. 4 *///小汽车类public class Car { 5 private String type; 6 private String color; 7 8 public String getType() { 9 return type; 10 } 11 12 public void setType(String type) { 13 this.type = type; 14 } 15 16 public String getColor() { 17 return color; 18 } 19 20 public void setColor(String color) { 21 this.color = color; 22 } 23 }
package cn.dawn.day02di; /** * Created by Dawn on 2018/3/3. *///学生类public class Stu { private String name; private Integer age; private Car car; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } }
配置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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--汽车--> <bean id="car" class="cn.dawn.day02di.Car"> <property name="type" value="奔驰"></property> <property name="color" value="红色"></property> </bean> <!--学生--> <!--这儿的小汽车不能用value,用ref引用上面的那个汽车car--> <bean id="stu" class="cn.dawn.day02di.Stu"> <property name="name" value="孟六"></property> <property name="age" value="20"></property> <property name="car" ref="car"></property> </bean> </beans>
测试类
package cn.dawn.day02; import cn.dawn.day02di.Stu; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by Dawn on 2018/3/3. */public class test20180303 { @Test /*域属性*/ public void t01(){ ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day02.xml"); Stu stu = (Stu) context.getBean("stu"); System.out.println(stu.getName()+"开"+stu.getCar().getType()); } }
第二个Demo:打印机案例
项目架构
1 package cn.dawn.day03printer.ink; 2 /** 3 * Created by Dawn on 2018/3/3. 4 *//*墨盒*/public interface Ink { 5 public String getInkColor(); 6 } 7 8 9 package cn.dawn.day03printer.ink; 10 /** 11 * Created by Dawn on 2018/3/3. 12 *//*彩色墨盒*/public class ColorInk implements Ink { 13 public String getInkColor() { 14 return "彩色墨盒"; 15 } 16 } 17 18 19 20 package cn.dawn.day03printer.ink; 21 /** 22 * Created by Dawn on 2018/3/3. 23 *//*黑白墨盒*/public class BlackInk implements Ink { 24 public String getInkColor() { 25 return "黑白墨盒"; 26 } 27 } 28 29 30 31 package cn.dawn.day03printer.paper; 32 /** 33 * Created by Dawn on 2018/3/3. 34 *//*纸张*/public interface Paper { 35 public String getPagerSize(); 36 } 37 38 39 40 package cn.dawn.day03printer.paper; 41 /** 42 * Created by Dawn on 2018/3/3. 43 *//*B5纸张*/public class B5Paper implements Paper{ 44 45 public String getPagerSize() { 46 return "B5纸"; 47 } 48 } 49 50 51 52 package cn.dawn.day03printer.paper; 53 /** 54 * Created by Dawn on 2018/3/3. 55 *//*A4纸张*/public class A4Paper implements Paper { 56 public String getPagerSize() { 57 return "A4纸"; 58 } 59 } 60 61 62 63 package cn.dawn.day03printer.printer; 64 65 import cn.dawn.day03printer.ink.Ink; 66 import cn.dawn.day03printer.paper.Paper; 67 /** 68 * Created by Dawn on 2018/3/3. 69 *//*打印机*/public class Printer { 70 /*墨盒*/ 71 private Ink ink; 72 /*纸张*/ 73 private Paper paper; 74 /*打印方法*/ 75 public void print(){ 76 System.out.println("我们的喷墨打印机,用"+ink.getInkColor()+"和"+paper.getPagerSize()+"打印出了------》我爱Spring"); 77 } 78 79 80 public Ink getInk() { 81 return ink; 82 } 83 84 public void setInk(Ink ink) { 85 this.ink = ink; 86 } 87 88 public Paper getPaper() { 89 return paper; 90 } 91 92 public void setPaper(Paper paper) { 93 this.paper = paper; 94 } 95 }
配置文件中:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--墨盒--> <bean id="ink" class="cn.dawn.day03printer.ink.ColorInk"></bean> <!--纸张--> <bean id="paper" class="cn.dawn.day03printer.paper.A4Paper"></bean> <!--打印机--> <bean id="printer" class="cn.dawn.day03printer.printer.Printer"> <property name="ink" ref="ink"></property> <property name="paper" ref="paper"></property> </bean> </beans>
单测方法
package cn.dawn.day03; import cn.dawn.day02di.Stu; import cn.dawn.day03printer.printer.Printer; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by Dawn on 2018/3/3. */public class test20180303 { @Test /*打印机案例*/ public void t01(){ ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day03.xml"); Printer printer = (Printer) context.getBean("printer"); printer.print(); } }