日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

接口的实现

發布時間:2024/10/12 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 接口的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、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

總結

以上是生活随笔為你收集整理的接口的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。