spring——控制反轉簡單例子,spring反轉例子

來源:互聯網
上載者:User

spring——控制反轉簡單例子,spring反轉例子

 spring架構是一個開源的輕量級的基於IOC與AOP核心技術的容器架構,主要是解決企業的複雜操作實現。

   那IOC與AOP,到底如何解釋呢,在看spring視頻中,兩個專業術語一定必須要懂得。

       IOC:inverse of Control:控制反轉。意思是程式中的之間的關係,不用代碼控制,而完全是由容器來控制。在運行階段,容器會根據配置資訊直接把他們的關係注入到組件中。同樣,這也是依賴注入的含義。依賴注入和控制反轉其實是一個概念。只不過強調的不同而已,依賴注入強調關係的注入是由容器在運行時完成,而控制反轉強調關係是由容器控制。其實本質是一樣的。

   用代碼示範一下控制反轉是如何?的。

1。建立一個普通的java項目。

2。引入相應的jar包。Spring.jar,log4j-1.2.14.jar(提供日誌功能的),commons-logging.jar

3。提供log4j.properties設定檔。(日誌jar包可以不用添加)

4。提供設定檔ApplicationContext.xml檔案

5。開始寫代碼。

  1. package ioc.iocsample;  
  2. /** 
  3.  * 學校類 
  4.  * @author lhy 
  5.  * 
  6.  */  
  7. public class School {  
  8.    private String name;  
  9.    public School(String name)  
  10.    {  
  11.        this.name=name;  
  12.    }  
  13.    public void printInfo()  
  14.    {  
  15.        System.out.println("該學校的名稱是:"+name);  
  16.    }  
  17. }  
  1. package ioc.iocsample;  
  2. /** 
  3.  * 學生類 
  4.  * @author lhy 
  5.  * 
  6.  */  
  7. public class Student {  
  8.   public int id;  
  9.   public String name;  
  10.   private School school;  
  11. public int getId() {  
  12.     return id;  
  13. }  
  14. public void setId(int id) {  
  15.     this.id = id;  
  16. }  
  17. public String getName() {  
  18.     return name;  
  19. }  
  20. public void setName(String name) {  
  21.     this.name = name;  
  22. }  
  23. public School getSchool() {  
  24.     return school;  
  25. }  
  26. public void setSchool(School school) {  
  27.     this.school = school;  
  28. }    
  29. }  

 
     設定檔:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.          xmlns:aop="http://www.springframework.org/schema/aop"  
  6.          xmlns:tx="http://www.springframework.org/schema/tx"  
  7.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  9.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  10.  <bean id="school" class="ioc.iocsample.School">  
  11.        <constructor-arg index="0">  
  12.          <value>廊坊師院</value>  
  13.        </constructor-arg>  
  14.  </bean>  
  15.  <bean id="student" class="ioc.iocsample.Student">  
  16.      <property name="id"      value="001"/>  
  17.      <property name="name" value="張三"/>  
  18.      <property name="school"  ref ="school"/>  
  19.  </bean>  
  20. </beans>  


    用戶端進行測試,其中學生類Student中有School中的引用,測試該學生就讀的學校的名稱如下:

  1. package ioc.iocsample;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class Client {  
  7.     public static void main(String[] args) {  
  8.         // TODO Auto-generated method stub  
  9.         BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");  
  10.         Student student=(Student)factory.getBean("student");  
  11.         student.getSchool().printInfo();  
  12.     }  
  13. }  


    其中,在程式中不用執行個體化學生類,學校類,直接由容器中的beanFactory直接建立,隱藏了建立了細節。同時,程式中也不用關心學生類與學校類之間的依賴關係,而由容器來進行負責,在啟動並執行時候,容器會把屬性值及依賴關係注入學生類和學校類中的javabean中(其實在此School和Student就是一個javaBean。javaBean就是一個按照一定的原則封裝的java類而已。)

   其中依賴注入包括兩種:一種賦值注入(使用getter和setter方法);另一種使用構造器注入。

   看程式中student中的ID,name都是使用get,set來賦值的:那在設定檔是如下配置:

       <property name="id"      value="001"/>
       <property name="name" value="張三"/>

   並且Student中的school屬性是School類型,則在容器中是如下配置的:

       <property name="school"  ref ="school"/>

   而在程式中的School中的name是構造器賦值的,則容器中是如下配置的:

      <constructor-arg index="0">
         <value>廊坊師院</value>
      </constructor-arg>

   構造器中一個參數,則索引值是從0開始,若是有多個,依次遞增。

    若構造器中的是一個類,則使用bean標籤

   <constructor-arg index="0">

    <bean class="具體的類">

  </constructor-arg>

  

   spring中的依賴注入DI(dependence injection)共有三種方式:第一種是介面注入(Interface Injection)第二種是get set注入(set/get Injection)第三種是構造器注入(Constructor Injection)

   三種注入方式的區別:

   1.介面注入:組件需要依賴特定介面的實現,其中的載入介面實現和介面實現的具體對象都是由容器來完成。這樣,介面必須依賴容器,這樣的組件具有侵入性,降低了重用性。其中如J2EE開發中常用的Context.lookup(ServletContext.getXXX),都是介面注入的表現形式。(這種注入方式不是常用的)

  

   2.getter/setter方式注入:對於需要注入的東西比較明確。符合java的設計規則。更適合java開發人員,使用起來更加自然,更加方便。

 

   3.構造器方式注入:在類載入的時候,就已經注入依賴的組件。但是若是參數多的話,使用起來不方便。

 

  但是後兩種注入方式是spring常用的,而第一種介面注入方式不常用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.