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

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. 配置安装nginx

    1.解决依赖关系和优化所需的组件 编译安装nginx需要事先需要安装开发包组"Development Tools" .同时,安装pcre-devel包,用yum安装即可 安装TCM ...

  2. 【15】-java实现二分查找

    二分查找在面试中经常被遇到,这个方法十分优雅 介绍 二分查找可以解决(预排序数组的查找)问题:只要数组中包含T(即要查找的值),那么通过不断缩小包含T的范围,最终就可以找到它.一开始,范围覆盖整个数组 ...

  3. LeetCode(43)-Contains Duplicate II

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  4. LeetCode(32)-Binary Tree Level Order Traversal

    题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...

  5. 详解PNG文件结构

    前言 PNG,JPEG,GIF,BMP作为数据压缩文件,有许多重要的信息我们需要区深度解析. 一.PNG的文件结构 1.1.数据块构成结构 PNG文件结构很简单,主要有数据块(Chunk Block) ...

  6. nginx日志中添加请求的response日志

    换个新公司,做一些新鲜的事情,经过一天的琢磨,终于成功添加response日志 在nginx的日志中添加接口response的日志 由于此功能在nginx内置的功能中没有,需要安装第三方模块ngx_l ...

  7. Vim PHP环境设置文章

    可能有重复: 在ubuntu 上配置vim的PHP开发环境 http://blog.csdn.net/robertaqi/article/details/6117546 手把手教你把Vim改装成一个I ...

  8. Flex编码随笔

    1.CSS定义最好放在application里面. 2.数据源是数组数据时,最好把数组转换为ArrayCollection. 3.List.CheckBox等控件的HttpService Params ...

  9. gevent程序员指南

    gevent程序员指南 由Gevent社区编写 gevent是一个基于libev的并发库.它为各种并发和网络相关的任务提供了整洁的API.   介绍 本指南假定读者有中级Python水平,但不要求有其 ...

  10. 设置firefox每次访问网页时检查所存网页的较新版本

    我们做技术,经常在写页面的时候需要多次刷新测试,可是浏览器都有自己的缓存机制,一般CSS和图片都会被缓存在本地,这样我们修改 的CSS就看不到效果了,每次都去清空缓存,再刷新看效果,这样操作太麻烦了. ...