DI:依赖注入

第一个DEMO:域属性注入

  java类:(Car类和Stu类,学生有一辆小汽车)

 package cn.dawn.day02di;
/**
* Created by Dawn on 2018/3/3.
*///小汽车类public class Car {
private String type;
private String color; public String getType() {
return type;
} public void setType(String type) {
this.type = type;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
}
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:打印机案例

  项目架构

 package cn.dawn.day03printer.ink;
/**
* Created by Dawn on 2018/3/3.
*//*墨盒*/public interface Ink {
public String getInkColor();
} package cn.dawn.day03printer.ink;
/**
* Created by Dawn on 2018/3/3.
*//*彩色墨盒*/public class ColorInk implements Ink {
public String getInkColor() {
return "彩色墨盒";
}
} package cn.dawn.day03printer.ink;
/**
* Created by Dawn on 2018/3/3.
*//*黑白墨盒*/public class BlackInk implements Ink {
public String getInkColor() {
return "黑白墨盒";
}
} package cn.dawn.day03printer.paper;
/**
* Created by Dawn on 2018/3/3.
*//*纸张*/public interface Paper {
public String getPagerSize();
} package cn.dawn.day03printer.paper;
/**
* Created by Dawn on 2018/3/3.
*//*B5纸张*/public class B5Paper implements Paper{ public String getPagerSize() {
return "B5纸";
}
} package cn.dawn.day03printer.paper;
/**
* Created by Dawn on 2018/3/3.
*//*A4纸张*/public class A4Paper implements Paper {
public String getPagerSize() {
return "A4纸";
}
} package cn.dawn.day03printer.printer; import cn.dawn.day03printer.ink.Ink;
import cn.dawn.day03printer.paper.Paper;
/**
* Created by Dawn on 2018/3/3.
*//*打印机*/public class Printer {
/*墨盒*/
private Ink ink;
/*纸张*/
private Paper paper;
/*打印方法*/
public void print(){
System.out.println("我们的喷墨打印机,用"+ink.getInkColor()+"和"+paper.getPagerSize()+"打印出了------》我爱Spring");
} public Ink getInk() {
return ink;
} public void setInk(Ink ink) {
this.ink = ink;
} public Paper getPaper() {
return paper;
} public void setPaper(Paper paper) {
this.paper = paper;
}
}

  配置文件中:

<?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();
}
}

最新文章

  1. 装饰模式(Decorator pattern)
  2. PHP审计小记
  3. qt5.4.0在windows,32位下的编译, vs2010平台
  4. Python在Windows上的安装
  5. TCP协议三次握手
  6. 让.NET程序快速释放内存的办法
  7. Scala 类和对象
  8. 微信小程序:微信登陆(ThinkPHP作后台)
  9. SpringMVC源码情操陶冶-AbstractUrlHandlerMapping
  10. Logistic Regression vs Naive Bayes
  11. 来聊一聊不low的Linux命令——find、grep、awk、sed
  12. Vim使用技巧:撤销与恢复撤销
  13. shell练习题7
  14. 【EMV L2】2CS.001.00 ~ 2CS.007.00
  15. ImageProcessor组件
  16. ajax之同步异步详解
  17. win10无法访问别的机器的共享目录
  18. (转)如何最佳地使用memcached?
  19. 利用POST重启路由器,一直无法实现,求帮助
  20. kubernetes 示例 hello world

热门文章

  1. 解决asp.net中HTML中talbe的行高被内容撑的变高的问题
  2. Toeplitz matrix
  3. 让linux进程后台运行、会话断开不退出
  4. 001-Eclipse、idea集成javap查看字节码、javap说明
  5. 024-Spring Boot 应用的打包和部署
  6. zabbix-2.4.8-1添加nginx状态监控
  7. uwsgi+nginx项目上线
  8. Shell Script Practice 2 Summary
  9. AFNetworking 和 ASIHTTPRequest
  10. 系统性能模块psutil