预备知识: Java反射原理,XML及其解析
IOC:Inversion of Control,控制反转,它最主要反映的是与传统面向对象(OO)编程的不同。通常我们编程实现某种功能都需要几个对象相互作用,从编程的角度出发,也就是一个主对象要保存其他类型对象的引用,通过调用这些引用的方法来完成任务。如何获得其他类型的对象引用呢?一种方式是主对象内部主动获得所需引用;另一种方式是在主对象中设置setter 方法,通过调用setter方法或构造方法传入所需引用。后一种方式就叫IOC,也是我们常常所说的依赖注入。以下我们用一个简单的例子来说明传统OO编程与IOC编程的差别。
这个例子的目的是根据时间不同返回不同的问候字符串, 比如Good Morning, world或Good afternoon, World。
服务接口:
package com.kettas.springdev.ioc;
public interface HelloIF {
String sayHello();
}
传统实现:
package com.kettas.springdev.ioc;
import java.util.Calendar;
public class HelloIFImpl implements HelloIF{
private Calendar cal; //我们需要的引用
public HelloIFImpl(){
cal = Calendar.getInstance(); //主动获取
}
public String sayHello(){
if(cal.get(Calendar.AM_PM) == Calendar.AM) return “Good morning, World”;
else return “Good afternoon, World”;
}
}
采用IOC方式:
package com.kettas.springdev.ioc;
import java.util.Calendar;
public class HelloIFImpl implements HelloIF{
private Calendar cal; //我们需要的引用
public void setCal(Calendar cal){ this.cal = cal;} //依赖注入
public String sayHello(){
if(cal.get(Calendar.AM_PM) == Calendar.AM) return “Good morning, World”;
else return “Good afternoon, World”;
}
}
在这里你也许会问:我看不出有太大差别,并且依赖注入还需要我先创建外部的Calendar对象,然后再传到HelloIFImpl对象中。
是的,如果我们直接创建HelloIFImpl对象没有任何的优势。如果我们让一个Bean工厂来帮我们创建HelloIF类型的引用就有优势了,当然要在这样的前提下: 1. Bean工厂可以随时改变HelloIF实现的类型;2. Bean工厂在创建好对象后主动调用依赖注入的方法。所以离开Bean工厂谈IOC是没有什么意义的, 开源框架Spring就提供了灵活多样的Bean工厂。以上例子可以通过如下XML片段来告诉Bean工厂如何创建对象并注入依赖:
<beans>
<bean id=”hello” class=”com.kettas.springdev.ioc.HelloIFImpl”> <!—调用构造方法产生对象à
<property name=”cal” > <!—注入下面定义的Calendar引用-->
<ref local=”calendar”/>
</property>
</bean>
<bean id=”calendar” class=”java.util.GregorianCalendar” /> <!—产生calendar对象à
</beans>
Bean工厂通过解析以上的配置就知道如何创建对象,如何注入依赖。
那Bean工厂到底如何实现所说的功能呢?从以上的XML配置片段我们可以看出
有两种数据类型:一个是Bean的定义(BeanDefinition);一个是对Bean的属性(Property)的定义(PropertyDefinition),嵌套在Bean定义中。Bean的定义包括:id, 类名(clazz)和一到多个PropertyDefinition; Property的定义包括: name, refName(如果是引用,指向另一个Bean), value(如果是基本数据类型), ifRef(是否是引用)。Java通过反射机制可以调用构造方法创建对象,也可以调用该实例上的方法。Bean工厂通过递归调用创建配置文件里定义的Bean对象,并调用这些对象的setter方法来实现依赖注入。而依赖注入都使用的是Bean的id, 所以我们在Bean工厂里用一个Map来保存Bean的定义,该Map的key是Bean的id。创建的Bean对象都是单例的,所以我们要保存Bean对象;而客户是通过id来获取Bean对象,所以我们也用Map来缓存。以下的代码提供了上述问题的解决方案(当然这是最简单的一种:只调用没有参数的构造方法,只实现基于setter的依赖注入,注入的依赖只能是基本数据类型或引用, xml解析没有做合法性检测等)
Bean的定义:BeanDefinition,java:
package com.kettas.springdev.ioc.bf;
import java.util.HashSet;
import java.util.Set;
public class BeanDefinition { //对应<bean>元素
private String clazz; //Bean类名全路径, 对应<bean>的attribute: class
private String id; //唯一属性, 对应<bean>的attribute: id
private Set<PropertyDefinition> propertyDefinitions //依赖描述, 对应<property>元素
= new HashSet<PropertyDefinition>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public Set<PropertyDefinition> getPropertyDefinitions() {
return propertyDefinitions;
}
public void setPropertyDefinitions(Set<PropertyDefinition> propertyDefinitions) {
this.propertyDefinitions = propertyDefinitions;
}
public void addPropertyDefinition(PropertyDefinition pd){
this.propertyDefinitions.add(pd);
}
}
Bean的属性的定义:PropertyDefinition.java:
package com.kettas.springdev.ioc.bf;
public class PropertyDefinition { //对应<property>元素
private String name; // <property> attribute: name
private String refName; // <property>子元素<ref>的attribute(如local, bean等)的值
private String value; //<property>子元素<value>的字符串子元素
private boolean isRef; //是否为引用
public boolean isRef() {
return isRef;
}
public void setRef(boolean isRef) {
this.isRef = isRef;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRefName() {
return refName;
}
public void setRefName(String refName) {
this.refName = refName;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Bean工厂接口: BeanFactory.java:
package com.kettas.springdev.ioc.bf;
public interface BeanFactory {
Object getBean(String id);
}
BeanFactory的一个实现:XmlBeanFactory.java:
package com.kettas.springdev.ioc.bf;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class XmlBeanFactory implements BeanFactory{
private Map<String, Object> beans
= new HashMap<String, Object>(); //Bean对象缓存
private Set<BeanDefinition> beanDefinitions
= new HashSet<BeanDefinition>(); //Bean的定义
public XmlBeanFactory(String xmlFileClassPath){
new BeanFactoryConfiguration(beanDefinitions, xmlFileClassPath);
}
public Object getBean(String id){
if(beans.containsKey(id)) return beans.get(id);
Object bean = null;
BeanDefinition bd = getBeanDefintion(id);
try{
Class beanClass = Class.forName(bd.getClazz());
bean = beanClass.newInstance();
for(PropertyDefinition pd : bd.getPropertyDefinitions()){
String property = pd.getName();
Method m = getSetter(beanClass,property);
//System.out.println(property + " : " + pd.isRef());
if(pd.isRef()){
m.invoke(bean, getBean(pd.getRefName()));
}else{
setValue(m, bean, pd.getValue());
}
}
beans.put(id, bean);
}catch(Exception e){
throw new RuntimeException("Can't create bean " + id, e);
}
return bean;
}
private void setValue(Method m, Object bean, String value) throws Exception {
// TODO Auto-generated method stub
Class paramType = m.getParameterTypes()[0];
//System.out.println(paramType.getName());
if(paramType == byte.class || paramType == Byte.class){
Byte b = new Byte(value);
m.invoke(bean, b);
}else if(paramType == short.class || paramType == Short.class){
Short s = new Short(value);
m.invoke(bean, s);
}else if(paramType == char.class || paramType == Character.class){
Character c = new Character(value.charAt(0));
m.invoke(bean, c);
}else if(paramType == int.class || paramType == Integer.class){
Integer i = new Integer(value);
m.invoke(bean, i);
}else if(paramType == float.class || paramType == Float.class){
Float f = new Float(value);
m.invoke(bean, f);
}else if(paramType == double.class || paramType == Double.class){
Double d = new Double(value);
m.invoke(bean, d);
}else{
m.invoke(bean, value);
}
}
private Method getSetter(Class beanClass, String property) throws Exception {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder("set");
char c = property.charAt(0);
if(c >= 'a' && c <= 'z') c -= 32;
sb.append(c).append(property.substring(1));
Method[] methods = beanClass.getMethods();
for(Method m : methods){
if(m.getName().equals(sb.toString()))
return m;
}
throw new RuntimeException("No such property: " + property);
}
private BeanDefinition getBeanDefintion(String id){
for(BeanDefinition bd : beanDefinitions){
if(bd.getId().equals(id))
return bd;
}
throw new RuntimeException("There is not [id="
+ id + "] in the xml configuration file");
}
}
解析Xml文件,生成BeanDefintion和PropertyDefinition: BeanFactoryConfiguration.java
package com.kettas.springdev.ioc.bf;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class BeanFactoryConfiguration {
public BeanFactoryConfiguration(Set<BeanDefinition> beanDefinitions, String xmlFileClassPath) {
// TODO Auto-generated constructor stub
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(
this.getClass().getResourceAsStream(xmlFileClassPath)
);
parse(doc, beanDefinitions);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException("can't parse the configuration file : " + xmlFileClassPath, e);
}
}
private void parse(Document doc, Set<BeanDefinition> beanDefinitions) {
// TODO Auto-generated method stub
NodeList beans = doc.getElementsByTagName("bean");
int len = beans.getLength();
for(int i = 0; i < len; i++){
BeanDefinition bd = new BeanDefinition();
Element n = (Element)beans.item(i);
NamedNodeMap nnm = n.getAttributes();
for(int j = 0; j < nnm.getLength(); j++){
Node attr = nnm.item(j);
//System.out.println(attr.getNodeName() + " " + attr.getNodeValue());
if(attr.getNodeName().equals("id"))
bd.setId(attr.getNodeValue());
else
bd.setClazz(attr.getNodeValue());
}
NodeList pros = n.getElementsByTagName("property");
for(int j = 0; j < pros.getLength(); j++){
bd.addPropertyDefinition(createPropertyDefinition(pros.item(j)));
}
beanDefinitions.add(bd);
}
}
private PropertyDefinition createPropertyDefinition(Node node) {
// TODO Auto-generated method stub
PropertyDefinition pd = new PropertyDefinition();
NamedNodeMap nnm = node.getAttributes();
pd.setName(nnm.item(0).getNodeValue());
NodeList nl = ((Element)node).getElementsByTagName("value");
if(nl != null && nl.getLength() > 0){
pd.setValue(nl.item(0).getFirstChild().getNodeValue());
}else{
pd.setRef(true);
nl = ((Element)node).getElementsByTagName("ref");
Node ref = nl.item(0);
pd.setRefName(ref.getAttributes().item(0).getNodeValue());
}
return pd;
}
}
测试程序:
package com.kettas.springdev.ioc.bf;
import springdev.ioc.day1.HelloIF;
public class TestMyBeanFactory {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BeanFactory bf = new XmlBeanFactory(
"/com/kettas/springdev/ioc/beans.xml"
);
HelloIF hello = (HelloIF)bf.getBean("hello");
System.out.println(hello.sayHello());
}
}
- 【java基础】IOC介绍及其简单实现
控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控制反转一般分为两种类型,依赖注入 ...
- 基于.NET CORE微服务框架 -surging的介绍和简单示例 (开源)
一.前言 至今为止编程开发已经11个年头,从 VB6.0,ASP时代到ASP.NET再到MVC, 从中见证了.NET技术发展,从无畏无知的懵懂少年,到现在的中年大叔,从中的酸甜苦辣也只有本人自知.随着 ...
- Spring升级案例之IOC介绍和依赖注入
Spring升级案例之IOC介绍和依赖注入 一.IOC的概念和作用 1.什么是IOC 控制反转(Inversion of Control, IoC)是一种设计思想,在Java中就是将设计好的对象交给容 ...
- 【转载】Ssh整合开发介绍和简单的登入案例实现
Ssh整合开发介绍和简单的登入案例实现 Ssh整合开发介绍和简单的登入案例实现 一 介绍: Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开 ...
- python模块介绍- HTMLParser 简单的HTML和XHTML解析器
python模块介绍- HTMLParser 简单的HTML和XHTML解析器 2013-09-11 磁针石 #承接软件自动化实施与培训等gtalk:ouyangchongwu#gmail.comqq ...
- WebRTC介绍及简单应用
WebRTC介绍及简单应用 WebRTC,即Web Real-Time Communication,web实时通信技术.简单地说就是在web浏览器里面引入实时通信,包括音视频通话等. WebRTC实时 ...
- 1. pyhanlp介绍和简单应用
1. pyhanlp介绍和简单应用 2. 观点提取和聚类代码详解 1. 前言 中文分词≠自然语言处理! 中文分词只是第一步:HanLP从中文分词开始,覆盖词性标注.命名实体识别.句法分析.文本分类等常 ...
- C#串口介绍以及简单串口通信程序设计实现
C#串口介绍以及简单串口通信程序设计实现 周末,没事干,写个简单的串口通信工具,也算是本周末曾来过,废话不多,直接到主题 串口介绍 串行接口简称串口,也称串行通信接口或串行通讯接口(通常指COM接口) ...
- 消息队列介绍、RabbitMQ&Redis的重点介绍与简单应用
消息队列介绍.RabbitMQ&Redis的重点介绍与简单应用 消息队列介绍.RabbitMQ.Redis 一.什么是消息队列 这个概念我们百度Google能查到一大堆文章,所以我就通俗的讲下 ...
随机推荐
- Redis笔记---set
1.redis set的介绍 集合中的数据是不重复且没有顺序,集合类型和列表类型的对比. 集合类型:存储的是的是最多2的32次方减一个字符串,数据是没有顺序的,但是数据是唯一的 列表类型:最多存储内容 ...
- UVA 11388 - GCD LCM 水~
看题传送门 题目大意: 输入两个数G,L找出两个正整数a 和b,使得二者的最大公约数为G,最小公倍数为L,如果有多解,输出a<=b且a最小的解,无解则输出-1 思路: 方法一: 显然有G< ...
- 链表(三)——链表删除冗余结点&插入结点到有序链表
1.一个以递增方式排列的链表,去掉链表中的冗余值. 思路一:设有两个指针p和q.使p不动,q依次往后循环直到p->data不等于q->data,再将中间的冗余数据删除. 思路二:设有两个指 ...
- 关于Android中设置闹钟的相对比较完善的解决方案
我当时说承诺为大家写一个,一直没空,直到最近又有人跟我要,我决定抽时间写一个吧.确实设置闹钟是一个比较麻烦的东西.我在这里写的这个demo抽出来了封装了一个类库,大家直接调用其中的设置闹钟和取消闹钟的 ...
- ArcGIS IQueryFilter接口
樱木 原文IQueryFilter 1.IQueryFilter::SubFields (1)默认值为“*”,即查询时返回整行数据,如果只需要某一个字段数据(比如“Country”字段),则可以指定 ...
- CleanCode代码整洁之道培训总结(2015-03-14)
为期四天的CleanCode培训时间非常短.非常难准确掌握一些知识.但让我对代码有了一个又一次的认识和启示:之前也有看过设计模式.重构之类的书,看完之后也有一些感触,过后在写代码中还是不能应用进来,事 ...
- Deep Learning for Nature Language Processing --- 第四讲(下)
A note on matrix implementations 将J对softmax的权重W和每一个word vector进行求导: 尽量使用矩阵运算(向量化).不要使用for loop. 模型训练 ...
- 【44.64%】【codeforces 743C】Vladik and fractions
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- JAVA中try-catch异常逃逸
有时候一些小的细节,确实比较纠结,对于try-catch-finally代码块中代码依次执行,当try中有exception抛出时,将会有catch拦截并执行,如果没有catch区块,那么except ...
- Multi-core compute cache coherency with a release consistency memory ordering model
A method includes storing, with a first programmable processor, shared variable data to cache lines ...