Spring的DI初步
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初步的更多相关文章
- SSM-Spring-02:Spring的DI初步加俩个实例
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- DI:依赖注入 第一个DEMO:域属性注入 java类:(Car类和Stu类,学生有一辆小汽车) packag ...
- Spring+IOC(DI)+AOP概念及优缺点
Spring pring是一个轻量级的DI和AOP容器框架. 说它轻量级有一大部分原因是相对与EJB的(虽然本人从没有接触过EJB的应用),重要的是,Spring是非侵入式的,基于spring开发的应 ...
- Spring特性--DI
DI:Dependency Injection(依赖注入),通俗的讲就是一种通过xml配置文件,为交给sping容器的对象初始化参数.又称做控制反转:Inversion of Control(IoC) ...
- spring+IOC+DI+AOP优点分析(一)
Spring是什么: Spring是一个轻量级的DI和AOP容器框架. 说它轻量级有一大部分原因是相对与EJB的(虽然本人从没有接触过EJB的应用),重要的是,Spring是非侵入式的,基于sprin ...
- spring ioc DI 理解
下面是我从网上找来的一些大牛对spring ioc和DI的理解,希望也能让你对Spring ioc和DI的设计思想有更进一步的认识. 一.分享Iteye的开涛对Ioc的精彩讲解 Ioc—Inversi ...
- [置顶] Spring的DI依赖实现分析
DI(依赖注入)是Spring最底层的核心容器要实现的功能之一,利用DI可以实现程序功能的控制反转(控制反转即程序之间之间的依赖关系不再由程序员来负责,而是由Spring容器来负责) 一个简单的例子( ...
- Spring IOC(DI)之注入方式
一次被问到IOC的注入方式,当时脑袋一阵混乱,不知道噻.于是google了一下,发现众说纷纭,有说三种的,有说四种的.都滚犊子吧,还是看看官方文档吧. DI exists in two major v ...
- Spring Ioc DI 原理
IOC(DI):其实这个Spring架构核心的概念没有这么复杂,更不像有些书上描述的那样晦涩.Java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用 ...
- spring的DI.IoC是什么
最近要搞spring的单元测试,不得已啊啊啊啊啊啊啊啊啊又要开始搞spring…… 日目晶…… 搞这几个概念,先甩一部分代码: UserDao 接口 package com.itheima.ioc; ...
随机推荐
- 1202 子序列个数(DP)
1202 子序列个数 题目来源: 福州大学 OJ 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 子序列的定义:对于一个序列a=a[1],a[2],......a[ ...
- jquery遍历json与数组方法总结
来自:http://www.php100.com/html/program/jquery/2013/0905/5927.html 先我们来参考each() 方法,each()规定为每个匹配元素规定运行 ...
- android菜鸟学习笔记26----Android广播消息及BroadcastReceiver
1.广播类型: Android中的广播有两种类型:标准广播和有序广播.其中,标准广播是完全异步发送的广播,发出之后,几乎所有的广播接收者都会在同一时刻收到这条广播消息,因而,这种类型的广播消息是不可拦 ...
- java.lang.ClassFormatError: Truncated class file
之前跑的很好的程序,因为我本地IDE出了问题的原因,倒是编译的错误的class文件,结果点击的时候报这样的错误,后来重新clean了工程,重新打包解压启动,问题依旧. 解决办法: 把tomcat的wo ...
- Token bucket
w https://en.wikipedia.org/wiki/Token_bucket
- dataTables的导出Excel功能
Datatables它是一款基于jQuery表格插件,钟情于它操作dom的灵活.做后台的同学想必使用它能事半功倍,而且交互强.容易扩展. 我也是最近要做公司后台界面,表格涉及的很多,所以考虑使用DT, ...
- CF85D Sum of Medians
CF85D Sum of Medians 题意翻译 一个集合,初始为空.现有三个操作: 1.add:向集合里加入数x,保证加入前集合中没有数x: 2.del:从集合中删除数x,保证删除前集合中有x: ...
- Python里面如何拷贝一个对象?
import copy lst=[1,2,3,4,[1,2]] # 复制列表lst,命名lst2 lst2=copy.copy(lst) print(f'这是lst3:{lst2}') # 深拷贝 l ...
- JavaScript数据结构-树
我认为这社会上,也不差钱好多人,可能好多人也不差权力.可是我认为能得到这样的满足的也不多. –郭小平<临汾红丝带学校校长> 树是计算机科学中经经常使用到的一种数据结构. 树是一种非线性 ...
- Impala SQL 语言元素(翻译)
摘要: http://www.cloudera.com/content/cloudera-content/cloudera-docs/Impala/latest/Installing-and-Usin ...