【spring】1.spring ioc原理和demo,1.springioc

來源:互聯網
上載者:User

【spring】1.spring ioc原理和demo,1.springioc

我們先做一個簡單的spring例子。


面向介面編程,我們先來定介面


IHelloWorld

package com.services;public interface IHelloWorld {public void sayHello();}

寫一個實作類別,並且聲明一個String屬性,提供set方法注入

package com.services.impl;import com.services.IHelloWorld;public class HelloWorld implements IHelloWorld {private String helloWorld;public void setHelloWorld(String helloWorld) {this.helloWorld = helloWorld;}@Overridepublic void sayHello() {System.out.println("hello world");}}

編寫設定檔

<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><bean id="helloWorldBean" class="com.services.impl.HelloWorld"><property name="helloWorld"><value>helloworld !!!</value></property></bean></beans>


注意看。設定檔的含義:

beans裡面是各種bean對象的聲明。

bean對象裡面描述了bean的資訊,裡麵包括bean名稱,對應的class檔案,class檔案裡的屬性和值。

編程測試檔案:

package com;import org.springframework.beans.factory.support.DefaultListableBeanFactory;import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;import org.springframework.core.io.ClassPathResource;import com.services.IHelloWorld;public class Test {/** * @param args */public static void main(String[] args) {ClassPathResource resource = new ClassPathResource("applicationContext.xml");DefaultListableBeanFactory factory = new DefaultListableBeanFactory();XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);reader.loadBeanDefinitions(resource);IHelloWorld hello = (IHelloWorld) factory.getBean("helloWorldBean");hello.sayHello();}}




ioc原理簡單分析:

我們看看測試代碼:


                //這裡其實是讀取xml檔案

ClassPathResource resource = new ClassPathResource(
"applicationContext.xml");

                //建立bean工廠,把讀取到的載入我們的xml的reader當中

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(resource);


//這裡其實是通過反射,獲得bean對象

IHelloWorld hello = (IHelloWorld) factory.getBean("helloWorldBean");
hello.sayHello();


簡述ioc原理:如果我們不使用spring,那麼bean對象建立就是通過new等方式硬代碼的方式寫入程式中。

ioc其實是把bean的資訊寫入xml檔案,通過jdom去解析到bean節點資訊,然后里麵包含bean的id或者name,還有bean對應的class全路徑,那麼我們通過getBean方法,就可以擷取到bean的類全路徑,通過反射就可以得到bean對象,程式內部其實是用hashMap來儲存xml解析出來的資訊的,hashMap的key其實就是我們寫在xml裡面的id,而值就是class對應的類全路徑,然後通過對象工廠,得到bean工廠後getBean就可以反射得到bean對象。類的建立過程是spring來幫你完成,我們自己的代碼中並未建立對象,也沒有對屬性賦值,整個過程spring容器完成,這也就是通常所說的依賴注入,或者說叫控制反轉(建立對象的過程是spring容器完成的,控制權發生了變化)


ioc其實裡面應用到了jcom(解析xml),反射(xml裡面的類全路徑後getBean得到對象),工廠設計模式。


部分源碼:


BeanFactory為頂層介面:該介面有很多常見的子介面和實作類別,大家可能還會看到很多其他人代碼用的XmlBeanFactory等子類實現,該介面有很多方法,我們常用獲得bean的方法getBean方法如下:






而大家代碼第一步擷取xml對象的方法,對應介面其實是Resource介面,裡面也有很多常見的實作類別。


太具體的源碼 大家自己看看api就都懂了  粗淺的原理還是很容易懂得

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.