------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

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=""></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:打印机案例

  先把架构放上来

      

  把里面每个类中的代码(code) 放上来

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

SSM-Spring-02:Spring的DI初步加俩个实例的更多相关文章

  1. Spring 02: Spring接管下的三层项目架构

    业务背景 需求:使用三层架构开发,将用户信息导入到数据库中 目标:初步熟悉三层架构开发 核心操作:开发两套项目,对比Spring接管下的三层项目构建和传统三层项目构建的区别 注意:本例中的数据访问层, ...

  2. 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制

    spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...

  3. SSM(spring mvc+spring+mybatis)学习路径——1-1、spring入门篇

    目录 1-1 Spring入门篇 专题一.IOC 接口及面向接口编程 什么是IOC Spring的Bean配置 Bean的初始化 Spring的常用注入方式 专题二.Bean Bean配置项 Bean ...

  4. ssm(spring,spring mvc,mybatis)框架

    ssm框架各个技术的职责 spring :spring是一个IOC DI AOP的 容器类框架 spring mvc:spring mvc 是一个mvc框架 mybatis:是一个orm的持久层框架 ...

  5. java web后台开发SSM框架(Spring+SpringMVC+MyBaitis)搭建与优化

    一.ssm框架搭建 1.1创建项目 新建项目后规划好各层的包. 1.2导入包 搭建SSM框架所需包百度云链接:http://pan.baidu.com/s/1cvKjL0 1.3整合spring与my ...

  6. 【SSM 8】spring集成Mybatis通用Mapper

    上篇博客中介绍了关于Mybatis底层封装的思路问题,那么这篇博客,就介绍一下怎么引入通用的mapper插件. 备注:本项目通过maven管理 关键版本说明: spring:4.1.3.RELEASE ...

  7. spring的IOC,DI及案例详解

    一:spring的基本特征 Spring是一个非常活跃的开源框架:它是一个基于Core来架构多层JavaEE系统的框架,它的主要目的是简化企业开发.Spring以一种非侵入式的方式来管理你的代码,Sp ...

  8. Spring MVC -- Spring框架入门(IoC和DI)

    Spring MVC是Spring框架中用于Web应用开发的一个模块.Spring MVC的MVC是Model-View-Controller的缩写.它是一个广泛应用于图像化用户交互开发中的设计模式, ...

  9. SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。

    SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...

随机推荐

  1. 生产者消费者的java实现

    先看最简单的,也就是缓冲区的容量为1 缓冲区容量为1 import java.util.List; public class ProducerAndConsumer2 { static class A ...

  2. 总结C语言在嵌入式开发中应用的知识点(文件数据的加密与解密)

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  3. NumberProgressBar开源项目学习

    1.概述 多看多学涨姿势, github真是个宝库 这个项目主要是实现数字进度条效果 github地址在https://github.com/daimajia/NumberProgressBar 感谢 ...

  4. Leetcode_190_Reverse Bits

    ), return 964176192 (represented in binary as00111001011110000010100101000000). 思路: (1)题意为给定无符号32位整数 ...

  5. Linux - /etc/passwd和/etc/shadow文件结构

    /etc/passwd文件结构 1.账号名称:         就是账号啦!用来对应 UID 的.例如 root 的 UID 对应就是 0 (第三字段):     2.口令:         早期 U ...

  6. Redis配置信息

    # Redis configuration file example # Note on units: when memory size is needed, it is possible to sp ...

  7. LeetCode(48)-Length of Last Word

    题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...

  8. Git学习备忘

    本文参考廖雪峰写的精彩的git学习文档,大家可以直接去官网看原版,我这里只是便于自己记录梳理 原版地址:http://www.liaoxuefeng.com/wiki/0013739516305929 ...

  9. TopShelf安装多实例

    Topshelf 安装多实例命令: .\ConsoleApp1.exe -instance "newinstallname" install 多实例有一个好处就是容灾,当一个服务部 ...

  10. 论MVC中的传值

    2个页面分别为Father.cshtml.Child.cshtml 2个控制器分别为FatherController.cs.ChildController.cs 1个js,为Father.js 一.F ...