仿照 Spring 實現簡單的 IOC 和 AOP - 上篇

1. 背景

我在大四實習的時候開始接觸 J2EE 方面的開發工做,也是在同時期接觸並學習 Spring 框架,到如今也有快有兩年的時間了。不過以前沒有仿寫過 Spring IOC 和 AOP,只是宏觀上對 Spring IOC 和 AOP 原理有必定的認識。因此爲了更進一步理解 Spring IOC 和 AOP 原理。在工做之餘,參考了一些資料和代碼,動手實現了一個簡單的 IOC 和 AOP,並實現了以下功能:java

  1. 根據 xml 配置文件加載相關 bean
  2. 對 BeanPostProcessor 類型的 bean 提供支持
  3. 對 BeanFactoryAware 類型的 bean 提供支持
  4. 實現了基於 JDK 動態代理的 AOP
  5. 整合了 IOC 和 AOP,使得兩者可很好的協同工做

在實現本身的 IOC 和 AOP 前,個人想法比較簡單,就是實現一個很是簡單的 IOC 和 AOP,哪怕是幾十行代碼實現的都行。後來實現後,感受還頗有意思的。不過那個實現太過於簡單,和 Spring IOC,AOP 相去甚遠。後來想了一下,不能僅知足那個簡單的實現,因而就有了這個仿寫項目。相對來講仿寫的代碼要複雜了一些,功能也多了一點,看起來也有點樣子的。儘管仿寫出的項目仍然是玩具級,不過寫仿寫的過程當中,仍是學到了一些東西。整體上來講,收穫仍是很大的。在接下來文章中,我也將從易到難,實現不一樣版本的 IOC 和 AOP。好了,很少說了,開始幹活。node

2. 簡單的 IOC 和 AOP 實現

2.1 簡單的 IOC

先從簡單的 IOC 容器實現開始,最簡單的 IOC 容器只需4步便可實現,以下:spring

  1. 加載 xml 配置文件,遍歷其中的 <bean> 標籤
  2. 獲取<bean>標籤中的 id 和 class 屬性,加載 class 屬性對應的類,並建立 bean
  3. 遍歷 <bean> 標籤中的 <property> 標籤,獲取屬性值,並將屬性值填充到 bean 中
  4. 將 bean 註冊到 bean 容器中

如上所示,僅需4步便可,是否是以爲很簡單。好了,Talk is cheap, Show me the code. 接下來要上代碼了。不過客官別急,上代碼前,容我對代碼結構作一下簡單介紹:框架

SimpleIOC     // IOC 的實現類,實現了上面所說的4個步驟
SimpleIOCTest    // IOC 的測試類
Car           // IOC 測試使用的 bean
Wheel         // 同上 
ioc.xml       // bean 配置文件

容器實現類 SimpleIOC 的代碼:ide

public class SimpleIOC {

    private Map<String, Object> beanMap = new HashMap<>();

    public SimpleIOC(String location) throws Exception {
        loadBeans(location);
    }

    public Object getBean(String name) {
        Object bean = beanMap.get(name);
        if (bean == null) {
            throw new IllegalArgumentException("there is no bean with name " + name);
        }

        return bean;
    }

    private void loadBeans(String location) throws Exception {
        // 加載 xml 配置文件
        InputStream inputStream = new FileInputStream(location);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        Document doc = docBuilder.parse(inputStream);
        Element root = doc.getDocumentElement();
        NodeList nodes = root.getChildNodes();

        // 遍歷 <bean> 標籤
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node instanceof Element) {
                Element ele = (Element) node;
                String id = ele.getAttribute("id");
                String className = ele.getAttribute("class");
                
                // 加載 beanClass
                Class beanClass = null;
                try {
                    beanClass = Class.forName(className);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                    return;
                }

                   // 建立 bean
                Object bean = beanClass.newInstance();

                // 遍歷 <property> 標籤
                NodeList propertyNodes = ele.getElementsByTagName("property");
                for (int j = 0; j < propertyNodes.getLength(); j++) {
                    Node propertyNode = propertyNodes.item(j);
                    if (propertyNode instanceof Element) {
                        Element propertyElement = (Element) propertyNode;
                        String name = propertyElement.getAttribute("name");
                        String value = propertyElement.getAttribute("value");

                            // 利用反射將 bean 相關字段訪問權限設爲可訪問
                        Field declaredField = bean.getClass().getDeclaredField(name);
                        declaredField.setAccessible(true);

                        if (value != null && value.length() > 0) {
                            // 將屬性值填充到相關字段中
                            declaredField.set(bean, value);
                        } else {
                            String ref = propertyElement.getAttribute("ref");
                            if (ref == null || ref.length() == 0) {
                                throw new IllegalArgumentException("ref config error");
                            }
                            
                            // 將引用填充到相關字段中
                            declaredField.set(bean, getBean(ref));
                        }

                        // 將 bean 註冊到 bean 容器中
                        registerBean(id, bean);
                    }
                }
            }
        }
    }

    private void registerBean(String id, Object bean) {
        beanMap.put(id, bean);
    }
}

容器測試使用的 bean 代碼:學習

public class Car {
    private String name;
    private String length;
    private String width;
    private String height;
    private Wheel wheel;
    
    // 省略其餘不重要代碼
}

public class Wheel {
    private String brand;
    private String specification ;
    
    // 省略其餘不重要代碼
}

bean 配置文件 ioc.xml 內容:測試

<beans>
    <bean id="wheel" class="com.titizz.simulation.toyspring.Wheel">
        <property name="brand" value="Michelin" />
        <property name="specification" value="265/60 R18" />
    </bean>

    <bean id="car" class="com.titizz.simulation.toyspring.Car">
        <property name="name" value="Mercedes Benz G 500"/>
        <property name="length" value="4717mm"/>
        <property name="width" value="1855mm"/>
        <property name="height" value="1949mm"/>
        <property name="wheel" ref="wheel"/>
    </bean>
</beans>

IOC 測試類 SimpleIOCTest:ui

public class SimpleIOCTest {
    @Test
    public void getBean() throws Exception {
        String location = SimpleIOC.class.getClassLoader().getResource("spring-test.xml").getFile();
        SimpleIOC bf = new SimpleIOC(location);
        Wheel wheel = (Wheel) bf.getBean("wheel");
        System.out.println(wheel);
        Car car = (Car) bf.getBean("car");
        System.out.println(car);
    }
}

測試結果:this

以上是簡單 IOC 實現的所有內容,難度不大,代碼也不難看懂,這裏再也不多說了。下面說說簡單 AOP 的實現。spa

2.2 簡單的 AOP 實現

AOP 的實現是基於代理模式的,這一點相信你們應該都知道。代理模式是AOP實現的基礎,代理模式不難理解,這裏就不花篇幅介紹了。在介紹 AOP 的實現步驟以前,先引入 Spring AOP 中的一些概念,接下來咱們會用到這些概念。

通知(Advice)

通知定義了要織入目標對象的邏輯,以及執行時機。
    Spring 中對應了 5 種不一樣類型的通知:
    · 前置通知(Before):在目標方法執行前,執行通知
    · 後置通知(After):在目標方法執行後,執行通知,此時不關係目標方法返回的結果是什麼
    · 返回通知(After-returning):在目標方法執行後,執行通知
    · 異常通知(After-throwing):在目標方法拋出異常後執行通知
    · 環繞通知(Around): 目標方法被通知包裹,通知在目標方法執行前和執行後都被會調用

切點(Pointcut)

若是說通知定義了在什麼時候執行通知,那麼切點就定義了在何處執行通知。因此切點的做用就是
    經過匹配規則查找合適的鏈接點(Joinpoint),AOP 會在這些鏈接點上織入通知。

切面(Aspect)

切面包含了通知和切點,通知和切點共同定義了切面是什麼,在什麼時候,何處執行切面邏輯。

說完概念,接下來咱們來講說簡單 AOP 實現的步驟。這裏 AOP 是基於 JDK 動態代理實現的,只需3步便可完成:

  1. 定義一個包含切面邏輯的對象,這裏假設叫 logMethodInvocation
  2. 定義一個 Advice 對象(實現了 InvocationHandler 接口),並將上面的 logMethodInvocation 和 目標對象傳入
  3. 將上面的 Adivce 對象和目標對象傳給 JDK 動態代理方法,爲目標對象生成代理

上面步驟比較簡單,不過在實現過程當中,仍是有一些難度的,這裏要引入一些輔助接口才能實現。接下來就來介紹一下簡單 AOP 的代碼結構:

MethodInvocation 接口  // 實現類包含了切面邏輯,如上面的 logMethodInvocation
Advice 接口        // 繼承了 InvocationHandler 接口
BeforeAdvice 類    // 實現了 Advice 接口,是一個前置通知
SimpleAOP 類       // 生成代理類
SimpleAOPTest      // SimpleAOP 從測試類
HelloService 接口   // 目標對象接口
HelloServiceImpl   // 目標對象

MethodInvocation 接口代碼:

public interface MethodInvocation {
    void invoke();
}

Advice 接口代碼:

public interface Advice extends InvocationHandler {}

BeforeAdvice 實現代碼:

public class BeforeAdvice implements Advice {
    private Object bean;
    private MethodInvocation methodInvocation;

    public BeforeAdvice(Object bean, MethodInvocation methodInvocation) {
        this.bean = bean;
        this.methodInvocation = methodInvocation;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 在目標方法執行前調用通知
        methodInvocation.invoke();
        return method.invoke(bean, args);
    }
}

SimpleAOP 實現代碼:

public class SimpleAOP {
    public static Object getProxy(Object bean, Advice advice) {
        return Proxy.newProxyInstance(SimpleAOP.class.getClassLoader(), 
                bean.getClass().getInterfaces(), advice);
    }
}

HelloService 接口,及其實現類代碼:

public interface HelloService {
    void sayHelloWorld();
}

public class HelloServiceImpl implements HelloService {
    @Override
    public void sayHelloWorld() {
        System.out.println("hello world!");
    }
}

SimpleAOPTest 代碼:

public class SimpleAOPTest {
    @Test
    public void getProxy() throws Exception {
        // 1. 建立一個 MethodInvocation 實現類
        MethodInvocation logTask = () -> System.out.println("log task start");
        HelloServiceImpl helloServiceImpl = new HelloServiceImpl();
        
        // 2. 建立一個 Advice
        Advice beforeAdvice = new BeforeAdvice(helloServiceImpl, logTask);
        
        // 3. 爲目標對象生成代理
        HelloService helloServiceImplProxy = (HelloService) SimpleAOP.getProxy(helloServiceImpl,beforeAdvice);
        
        helloServiceImplProxy.sayHelloWorld();
    }
}

輸出結果:

以上實現了簡單的 IOC 和 AOP,不過實現的 IOC 和 AOP 還很簡單,且只能獨立運行。在下一篇文章中,我將實現一個較爲複雜的 IOC 和 AOP,你們若是有興趣能夠去看看。好了,本篇文章到此結束。

本文在知識共享許可協議 4.0 下發布,轉載請註明出處
做者:coolblog
爲了得到更好的分類閱讀體驗,
請移步至本人的我的博客: http://www.coolblog.xyz

cc
本做品採用知識共享署名-非商業性使用-禁止演繹 4.0 國際許可協議進行許可。

相關文章
相關標籤/搜索