接口的实现
一、IOC的意思是控件反轉也就是由容器控制程序之間的關系,把控件權交給了外部容器,之前的寫法,由程序代碼直接操控,而現在控制權由應用代碼中轉到了外部容器,控制權的轉移是所謂反轉。DI(Dependency Injection,依賴注入)。IoC的一個重點是在系統運行中,動態的向某個對象提供它所需要的其他對象。這一點是通過DI(Dependency Injection,依賴注入)來實現的。
IOC實現的原理:
?
定義DAO接口和接口的實現類
?
package?com.dao;?
public?interface?PersonDAO?{??
public?void?save();??
}??
package?com.dao.impl;???
import?com.dao.PersonDAO;??
public?class?PersonDaoImpl?implements?PersonDAO?{??
????@Override??
????public?void?save()?{??
????System.out.println("保存");??
????}?
}?
創建一個Junit測試類
?
package?com.test;??
import?org.junit.Test;??
import?org.springframework.context.ApplicationContext;??
import?org.springframework.context.support.ClassPathXmlApplicationContext;??
import?com.dao.PersonDAO;??
import?com.myUtil.MyClassPathXmlApplicationContext;??
import?com.service.PersonService;?
public?class?PersonTest?{??
????@Test??
????public?void?instanceSpring1(){??
??????/*
?????????*?spring?的實現?
?????????*/??
????????//IOC??
????????ApplicationContext?ac?=?new?ClassPathXmlApplicationContext("beans.xml");??
????????PersonDAO?pd?=?(PersonDAO)?ac.getBean("personDAO");??
????????pd.save();??
????????//DI??
????????PersonService?ps?=?(PersonService)?ac.getBean("personService");??
????????ps.save();??
????}??
????@Test??
????public?void?instanceSpring2(){??
????????/**?
?????????*?我的實現?
????????????????*/??
????????MyClassPathXmlApplicationContext?mac?=?new?MyClassPathXmlApplicationContext("beans.xml");??
????????PersonDAO?mpd?=?(PersonDAO)?mac.getBean("personDAO");??
????????mpd.save();??
????????//DI??
????????PersonService?ps?=?(PersonService)?mac.getBean("personService");??
????????ps.save();??
????}?????
}??
?
方法instanceSpring1為Spring中的實現用ClassPathXmlApplicationContext類,要實現IOC的原理要定義自己的MyClassPathXmlApplicationContext首先讀出beans.xml中的配置信息,通過反射機制實現bean,最后注入所需要的bean。
?
package com.myUtil;?
import java.beans.Introspector;?
import java.beans.PropertyDescriptor;?
import java.lang.reflect.Method;?
import java.net.URL;?
import java.util.ArrayList;?
import java.util.HashMap;?
import java.util.List;?
import java.util.Map;?
import org.dom4j.Document;?
import org.dom4j.Element;?
import org.dom4j.io.SAXReader;?
public class MyClassPathXmlApplicationContext {?
??? // xml所有的屬性?
??? private ArrayList<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();?
??? // xml中所有的bean?
??? private Map<String, Object> sigletons = new HashMap<String, Object>();?
??? public MyClassPathXmlApplicationContext(String file) {?
??????? readXml(file);?
??????? instanceBeans();?
??????? instanceObject();?
??? }?
??? /**
???? * 注入
???? */?
??? private void instanceObject() {?
??????? for (BeanDefinition beanDefinition : beanDefinitions) {?
??????????? //判斷有沒有注入屬性?
??????????? if (beanDefinition.getProperty() != null) {?
??????????????? Object bean = sigletons.get(beanDefinition.getId());?
??????????????? if (bean != null) {?
??????????????????? try {?
??????????????????????? //得到被注入bean的所有的屬性?
???????????? ???????????PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();?
??????????????????????? //得到所有的注入bean屬性?
?? ?for(PropertyDefinition propertyDefinition:beanDefinition.getProperty()){?
? ???????????????for(PropertyDescriptor propertyDescriptor:ps){?
??????????? if(propertyDescriptor.getName().equals(propertyDefinition.getName())){?
??? ???????Method setter = propertyDescriptor.getWriteMethod();//獲取set方法?
??????????????????????????????????? if(setter!=null){?
? ???????????????????????????setter.setAccessible(true);//得到private權限?
??????????????????????????????????????? //注入屬性?
?????? ?????????????????????????setter.invoke(bean, sigletons.get(propertyDefinition.getRef()));?
??????????????????????????????????? } ?
??????????????????????????????????? break;?
??????????????????????????????? }?
??????????????????????????? }?
??????????????????????? }?
??????????????????? } catch (Exception e) {?
??????????????????????? // TODO Auto-generated catch block?
????? ??????????????????e.printStackTrace();?
??????????????????? }?
??????????????? }? ?}?
??????? }? ?}? ?
??? /**
???? * 實例所有的bean
???? */?
??? private void instanceBeans() {?
??????? for (int i = 0; i < beanDefinitions.size(); i++) {?
??????????? BeanDefinition bd = beanDefinitions.get(i);?
??????????? try {?
??????????????? try {?
??????????????????? if (bd.getClassName() != null?
??????????????????????????? && !bd.getClassName().equals(""))?
??????????????????????? sigletons.put(bd.getId(), Class.forName(?
??????????????????????????????? bd.getClassName()).newInstance());?
??????????????? } catch (InstantiationException e) {?
??????????????????? // TODO Auto-generated catch block?
??????????????????? e.printStackTrace();?
??????????????? } catch (IllegalAccessException e) {?
??????????????????? // TODO Auto-generated catch block?
??????????????????? e.printStackTrace();?
??????????????? }?
??????????? } catch (ClassNotFoundException e) {?
??????????????? // TODO Auto-generated catch block?
??????????????? e.printStackTrace();?
??????????? }?
??????? }? ??}?
??? /**
???? * 讀xml
???????? * @param file
???? */?
??? private void readXml(String file) {?
??????? try {?
??????????? SAXReader reader = new SAXReader(); // 使用SAX方式解析XML?
??????????? URL xmlPath = this.getClass().getClassLoader().getResource(file);?
??????????? Document doc = reader.read(xmlPath);?
??????????? Element root = doc.getRootElement(); // 取得根節點?
??????????? List<Element> beans = root.elements();?
??????????? for (Element element : beans) {?
??????????????? String id = element.attributeValue("id");// id;?
????????????? ??String clazz = element.attributeValue("class");?
??????????????? BeanDefinition bd = new BeanDefinition(id, clazz);?
??????????????? // 讀取子元素?
??????????????? if (element.hasContent()) {?
??????????????????? List<Element> propertys = element.elements();?
??????????????????? for (Element property : propertys) {?
??????????????????????? String name = property.attributeValue("name");?
??????????????????????? String ref = property.attributeValue("ref");?
??????????????????????? PropertyDefinition pd = new PropertyDefinition(name,?
??????????????????????????????? ref);?
??????????????????????? bd.getProperty().add(pd);?
??????????????????? }? ???}?
??????????????? beanDefinitions.add(bd);?
??????????? }?
??????? } catch (Exception e) {?
???????? ???// TODO: handle exception?
??????? }?
??? }?
??? /**
???? * 通過名字得到bean
???? * @param str
???? * @return
???? */?
??? public Object getBean(String str) {?
??????? return sigletons.get(str);?
??? } }
?
讀取所的bean實體
?
package com.myUtil; ?
import java.util.ArrayList;?
import java.util.List;?
public class BeanDefinition {?
??? private String id;?
??? private String className;?
??? private List<PropertyDefinition> property = new ArrayList<PropertyDefinition>();?
??? public BeanDefinition(String id, String className) {?
??????? super();?
??????? this.id = id;?
??????? this.className = className;?
??? }?
??? public String getId() {?
??????? return id;?
??? }?
??? public void setId(String id) {?
??????? this.id = id;?
??? }?
??? public String getClassName() {?
??????? return className;?
??? }?
??? public void setClassName(String className) {?
??????? this.className = className;?
??? }?
??? public List<PropertyDefinition> getProperty() {?
??????? return property;?
??? }?
??? public void setProperty(List<PropertyDefinition> property) {?
??????? this.property = property;?
??? }?
}?
?
注入屬性實體
?
[java] view plain copy
package com.myUtil;?
?
public class PropertyDefinition {?
??? private String name;?
??? private String ref;?
? ??public PropertyDefinition(String name, String ref) {?
??????? this.name = name;?
??????? this.ref = ref;?
??? }?
??? public String getName() {?
??????? return name;?
??? }?
??? public void setName(String name) {?
??????? this.name = name;?
??? }
??? public String getRef() {?
??????? return ref;?
??? }?
??? public void setRef(String ref) {?
??????? this.ref = ref;?
??? }?
}?
?
業務接口和實現類
?
package com.service;
public interface PersonService {?
??? public void save();?
}?
[java] view plain copy
package com.service.impl;? ?
import com.dao.PersonDAO;?
import com.service.PersonService;? ??
public class PersonServiceImpl implements PersonService{?
??? private PersonDAO pdo;?
?
??? public PersonDAO getPdo() {?
??????? return pdo;?
??? }?
??? public void setPdo(PersonDAO pdo) {?
??????? this.pdo = pdo;?
??? }?
??? @Override?
??? public void save() {?
??????? pdo.save();?
??? }?
}?
beans.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-2.5.xsd">? ?
??? <bean id="personDAO" class="com.dao.impl.PersonDaoImpl"></bean>?
??? <bean id="personService" class="com.service.impl.PersonServiceImpl">?
??????? <property name="pdo" ref="personDAO"></property>?
??? </bean>?
</beans>?
轉載于:https://www.cnblogs.com/zhangshuyiaa/p/7095098.html
總結
- 上一篇: 【行为型模式】《大话设计模式》——读后感
- 下一篇: 【翻译自mos文章】OGG的集成捕捉模式