Spring基礎[IOC/DI、AOP]

1、Spring做用:管理項目中各類業務Bean(service類、Dao類、Action類),實例化類,屬性賦值spring

2、Spring IOC(Inversion of Control )控制反轉,也被稱爲依賴注入(Dependency Injection[DI]),是面向對象編程 中的一種設計理念,用來減輕程序代碼之間的耦合度。編程

IOC原理:側重原理app

在業務代碼中不使用咱們之間司空見慣的關鍵字new來構建一個業務實例,而是在配置文件中。經過xml節點來告知容器如何對內存中構建的對應類型的對象名稱作命名測試

DI依賴注入:側重實現spa

 說的是建立對象實例時,爲這個對象注入屬性值其它對象實例,側重於實現。設計

 二者關係它們是spring核心思想的不一樣方面的描述。3d


3、Spring第一個案例[無需new關鍵字,建立Student對象]代理

具體步驟:日誌

①建立Student實體類code

②在src下建立applicationContext.xml配置文件

③ 測試類

---引入核心jar包:[Beans、Core、Context、Expression]

 

---建立實體類Student 可植入相應的屬性值

 

---在src下編寫Spring配置文件

頭文件查找位置:源碼

applicationContext.xml:

注:在spring配置文件中,使用<bean>元素來定義Bean實例(也可稱爲組件)。有兩個經常使用屬性:一個是id,表示定義的Bean實例的名稱,另外一個是Class,表示定義的Bean實例的類型

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="
 5         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 6         
 7         <bean id="stu" class="cn.happy.entity.Student">
 8         <property name="name" value="呵呵"></property>
 9         <property name="age" value="13"></property>
10        
11        </bean>
12        
13         </beans>

 --進行測試Test

經過ClassPathXmlApplicationContext實例化Spring的上下文,並經過ApplicationContext的getBean()方法,根據id來獲取Bean實例。ApplicationContext是一個接口,負責讀取Spring配置文件,管理對象的加載,生成,維護Bean對象與Bean對象之間的依賴關係,負責Bean的生命週期。ClassPathXmlApplicationContext是ApplicationContext接口的實現類,用於從classpath路徑中讀取spring配置文件。

 

1 public class Test01 {
2 @Test
3 public void addTest(){
4     ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
5     Student stus=(Student) ctx.getBean("stu");    
6     System.out.println(stus);
7     
8 }

 輸出結果:

 


4、依賴注入[DI]

經過不一樣的紙張和不一樣類型墨盒的組合,來裝配出一臺打印機。

 具體步驟:

①分層建立墨盒和紙張接口

②實現類

③在配置文件中裝配

 --ink包下建立Ink接口和其實現類

 ink接口:

public interface Ink {
    public String getColor();
}

實現類:

public class ColorInk implements Ink{

    public String getColor() {
        
        return "彩色墨盒";
    }

}
public class BlackInk implements Ink{

    public String getColor() {
        
        return "黑色墨盒";
    }

}

 --在paper包下建立paper接口和其實現類

 paper接口:

public interface Paper {
    public String getContent();
}

實現類: 

public class A4Paper implements Paper{

    public String getContent() {        
        return "我是A4";
    }

}
public class B5Paper implements Paper{
    public String getContent() {
        return "我是B5";
    }

}

--printer包下建立Print類,並植入ink,paper成員變量,稱爲域屬性。封裝,以及打印的方法。

public class Print {
    private Ink ink;
    private Paper paper;
    
    public void print(){
        System.out.println(""+ink.getColor()+"打印"+paper.getContent()+"紙張");
    }
    

 --配置文件,並對ink和paper以及打印機進行配置

--測試類。強轉的是打印機類print

1 public class SpringTest {
2     @Test
3     public void test1(){
4         ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
5         Print pri=(Print) ctx.getBean("printer");
6         pri.print();
7     }

輸出結果:

 


5、Spring AOP[Aspect Oriented Programming] 面向切面編程

AOP原理:

將複雜的需求分解出不一樣方面,將散佈在系統中的公共功能集中解決

採用代理機制組裝起來運行,在不改變原程序的基礎上對代碼段進行加強處理,增長新的功能

AOP目的:

從系統中分離出切面,獨立於業務邏輯實現

 

如何使用AOP?

具體步驟:

①在項目中添加Spring AOP相關的JAR文件

②編寫前置加強和後置加強實現日誌功能

③編寫Spring文件,對業務方法進行加強處理

④編寫代碼獲取帶有加強處理的業務對象

--添加相關jar文件:

--業務類:並實現特定接口,在該方法中並無實現日誌輸出功能

ISomeService:

SomeService:

 

 --接下來就以AOP方式爲該方法添加日誌功能,編寫加強類

經過 MethodBeforeAdvice實現前置加強

經過AfterReturningAdvice實現後置加強

 

--Spring配置文件進行組件聲明。

注:頭文件需引入對應的aop

--接下來在Spring配置文件中實現Aop配置,首先定義切入點。execution是切入點指示符,它的括號中是一個切入點表達式,能夠配置要切入的方法,切入點表達式支持模糊匹配

 

--測試類

標紅注意點:

 關於接口的一個問題:

若是一個類實現了一個接口,默認使用的代理是JDK動態代理

若是一個類沒有實現任何接口,默認使用的是cglib動態代理

 在這裏假若強轉的是類SomeService類,則會報以下錯誤:

 

 實現效果:

 

相關文章
相關標籤/搜索