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

Spring的DI初步的更多相关文章

  1. SSM-Spring-02:Spring的DI初步加俩个实例

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- DI:依赖注入 第一个DEMO:域属性注入 java类:(Car类和Stu类,学生有一辆小汽车) packag ...

  2. Spring+IOC(DI)+AOP概念及优缺点

    Spring pring是一个轻量级的DI和AOP容器框架. 说它轻量级有一大部分原因是相对与EJB的(虽然本人从没有接触过EJB的应用),重要的是,Spring是非侵入式的,基于spring开发的应 ...

  3. Spring特性--DI

    DI:Dependency Injection(依赖注入),通俗的讲就是一种通过xml配置文件,为交给sping容器的对象初始化参数.又称做控制反转:Inversion of Control(IoC) ...

  4. spring+IOC+DI+AOP优点分析(一)

    Spring是什么: Spring是一个轻量级的DI和AOP容器框架. 说它轻量级有一大部分原因是相对与EJB的(虽然本人从没有接触过EJB的应用),重要的是,Spring是非侵入式的,基于sprin ...

  5. spring ioc DI 理解

    下面是我从网上找来的一些大牛对spring ioc和DI的理解,希望也能让你对Spring ioc和DI的设计思想有更进一步的认识. 一.分享Iteye的开涛对Ioc的精彩讲解 Ioc—Inversi ...

  6. [置顶] Spring的DI依赖实现分析

    DI(依赖注入)是Spring最底层的核心容器要实现的功能之一,利用DI可以实现程序功能的控制反转(控制反转即程序之间之间的依赖关系不再由程序员来负责,而是由Spring容器来负责) 一个简单的例子( ...

  7. Spring IOC(DI)之注入方式

    一次被问到IOC的注入方式,当时脑袋一阵混乱,不知道噻.于是google了一下,发现众说纷纭,有说三种的,有说四种的.都滚犊子吧,还是看看官方文档吧. DI exists in two major v ...

  8. Spring Ioc DI 原理

    IOC(DI):其实这个Spring架构核心的概念没有这么复杂,更不像有些书上描述的那样晦涩.Java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用 ...

  9. spring的DI.IoC是什么

    最近要搞spring的单元测试,不得已啊啊啊啊啊啊啊啊啊又要开始搞spring…… 日目晶…… 搞这几个概念,先甩一部分代码: UserDao 接口 package com.itheima.ioc; ...

随机推荐

  1. 【BZOJ4974】字符串大师 KMP

    [BZOJ4974]字符串大师 Description 一个串T是S的循环节,当且仅当存在正整数k,使得S是T^k(即T重复k次)的前缀,比如abcd是abcdabcdab的循环节.给定一个长度为n的 ...

  2. 1282 时钟(最小表示法+hash)

    1282 时钟 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N个时钟,每个时钟有M个指针,P个刻度.时钟是圆形的,P个刻度均分整 ...

  3. JavaScript数据结构与算法-列表练习

    实现列表类 // 列表类 function List () { this.listSize = 0; // 列表的元素个数 this.pos = 0; // 列表的当前位置 this.dataStor ...

  4. Servlet 3.0 介绍

    1. 概述 注解代替 web.xml 配置文件 异步处理 对上传的支持 2. 注解代替 web.xml 配置文件 使用方法 在 Servlet 类上添加 @WebServlet(urlPatterns ...

  5. 前端基础 DOM & BOM

    推荐阅读:http://www.cnblogs.com/yuanchenqi/articles/6893904.html#_label3 BOM对象 window 对象 所有浏览器都支持 window ...

  6. (扫盲)C#中out和ref之间的区别

    首先:两者都是按地址传递的,使用后都将改变原来参数的数值. 其次:ref可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所 ...

  7. Logstash简介

    支持多种数据获取机制,通过TCP/UDP协议.文件.syslog.windows.EventLogs及STDIN等:获取到数据口,支持对数据进行过滤.修改等操作JRuby语言研发,工作在JVM中   ...

  8. 【WEB HTTP】缓存

    1. HTTP并不支持兄弟缓存,所以人们通过一些协议对HTTP进行了扩展,比如因特网缓存协议(Internet Cache Protocol, ICP)和超文本缓存协议(HyperText Cachi ...

  9. Shallow Copy & Deep Copy in Python list

    今天在写一个小程序的时候用到了2维数组, 顺手就写成了[[0.0]*length]*length, 结果为了这个小错,调试了半个多小时, 其实之前对与浅复制和深复制已经做过学习和总结, 但真正编程用到 ...

  10. OpenGL学习进程(9)在3D空间的绘制实例

        本节将演示在3D空间中绘制图形的几个简单实例:     (1)在3D空间内绘制圆锥体: #include <GL/glut.h> #include <math.h> # ...