spring框架學習(一)——IOC/DI

什麼是Spring框架:html

  Spring是一個基於IOC和AOP的結構J2EE系統的框架:java

  IOC 反轉控制 是Spring的基礎,Inversion Of Control,簡單說就是建立對象由之前的程序員本身new 構造方法來調用,變成了交由Spring建立對象;程序員

  DI 依賴注入 Dependency Inject, 簡單地說就是拿到的對象的屬性,已經被注入好相關值了,直接使用便可。spring

原理分析:app

  以獲取對象的方式來進行比較

    傳統的方式:
      經過new 關鍵字主動建立一個對象。
    IOC方式:
      對象的生命週期由Spring來管理,直接從Spring那裏去獲取一個對象。 IOC是反轉
控制 框架

      (Inversion Of Control)的縮寫,就像控制權從原本在本身手裏,交給了Spring。ide

  打個比喻:
    傳統方式:至關於你本身去菜市場new 了一隻雞,不過是生雞,要本身拔毛,去內臟,再上
測試

         花椒,醬油,烤制,通過各類工序以後,才能夠食用。
    用 IOC:至關於去館子(Spring)點了一隻雞,交到你手上的時候,已經五味俱全,你就只管
this

        吃就好了。spa

                  

 

 

接下來經過運行TestSpring演示如何用Spring獲取一個對象,並打印其name以及使用IOC的方式,對Product對象,注入一個Category對象

(建立項目以及導包過程已省略,關於spring項目中須要的jar包介紹請點擊此處

準備pojo Category 、Product,用來演示IOC和DI:

package com.spring.pojo; public class Category { public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } private int id; private String name; }

 

package com.spring.pojo; public class Product { private int id; private String name; private Category category;  public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } }

 

建立applicationContext.xml:

    在src目錄下新建applicationContext.xml文件;
    applicationContext.xml是Spring的核心配置文件,經過關鍵字p便可獲取
Product對象

    (IOC過程)該對象獲取的時候,即被注入了字符串"product 1「到name屬性中(DI過程)

    在建立Product的時候注入一個Category對象,注意,這裏要使用ref來注入另外一個對象(DI過程)

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="    http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
 <bean name="c" class="com.spring.pojo.Category"> <property name="name" value="category 1" /> </bean>
   < bean name="p" class="com.how2java.pojo.Product">
       < property   name = "name"   value = "product1"   />
            <!--使用ref注入category對象-->
       <property name="category" ref="c" />
     </bean>
</beans>

 

TestSpring:
    測試代碼,演示經過spring獲取product對象,以及該對象被注入的name和category屬性。如圖所示,能夠打印
    出經過Spring拿到的Product對象的name屬性和Category對象的name屬性。
          
package com.spring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.pojo.Product; public class TestSpring { public static void main(String[] args) { //讀取配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext(           new String[] { "applicationContext.xml" }); //經過關鍵字「c」,從spring中獲取Category對象
        Product p = (Product) context.getBean("p");
     
//p.getName()獲取p對象中屬性name的值,p.getCategory().getName()獲取category的name值 System.out.println(p.getName());
     
System.out.println(p.getCategory().getName());
}
}

 注:讀取配置文件:

//方式一:(只能讀取一個配置文件)
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //方式二:(可讀取一個或多個配置文件)
ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" });

 

原文地址:https://how2j.cn/k/spring/spring-injection/88.html#nowhere

相關文章
相關標籤/搜索