1. 程式人生 > >java框架之spring(四)

java框架之spring(四)

AOP 面向切面程式設計

AOP 即 Aspect Oriented Program 面向切面程式設計 
首先,在面向切面程式設計的思想裡面,把功能分為核心業務功能周邊功能。 

所謂的核心業務,比如登陸,增加資料,刪除資料都叫核心業務 

所謂的周邊功能,比如效能統計,日誌,事務管理等等 
周邊功能在Spring的面向切面程式設計AOP思想裡,即被定義為切面 
在面向切面程式設計AOP的思想裡面,核心業務功能和切面功能分別獨立進行開發 
然後把切面功能和核心業務功能 "編織" 在一起,這就叫AOP


為了支援AOP,需要用到一些額外的JAR包。 這些額外的JAR包放在專案的lib目錄下。 


至於多了哪些,記不太清楚了。。。。 都匯入吧。。。。


思路圖

1. 功能分兩大類,輔助功能和核心業務功能
2. 輔助功能和核心業務功能彼此獨立進行開發
3. 比如登陸功能,即便是沒有效能統計和日誌輸出,也可以正常執行
4. 如果有需要,就把"日誌輸出" 功能和 "登陸" 功能 編織在一起,這樣登陸的時候,就可以看到日誌輸出了
5. 輔助功能,又叫做切面,這種能夠選擇性的,低耦合的把切面和核心業務功能結合在一起的程式設計思想,就叫做切面程式設計


接下去繼續在上個專案中進行改造

1、準備業務類 ProductService

package com.hjsy.service;

public class ProductService {
	public void doSomeService(){
        System.out.println("doSomeService");
    }
}
2、 在引入切面之前,呼叫該業務類
package spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hjsy.pojo.Category;
import com.hjsy.pojo.Product;
import com.hjsy.service.ProductService;

public class TestSpring {
	public static void main(String[] args) {
		ApplicationContext context1 = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });;
		ProductService s = (ProductService) context1.getBean("hjsy2");
        	
s.doSomeService(); } }
3、執行輸出結果


4、準備日誌切面 LoggerAspect

該日誌切面的功能是 在呼叫核心功能之前和之後分別列印日誌。

package com.hjsy.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class LoggerAspect {
	public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

Object object = joinPoint.proceed(); 就是將來與某個核心功能編織之後,用於執行核心功能的程式碼

5、修改applicationContext.xml檔案

(1)新增

<bean name="hjsy2" class="com.hjsy.service.ProductService">
</bean>
(2) 宣告業務物件
 <bean id="loggerAspect" class="com.hjsy.aspect.LoggerAspect"/>
(3) 宣告日誌切面
<aop:pointcut id="loggerCutpoint"
            expression=
            "execution(* com.hjsy.service.ProductService.*(..)) "/>
(4) 指定核心業務功能
<aop:aspect id="logAspect" ref="loggerAspect">
	<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
(5) 指定輔助功能
然後通過aop:config把業務物件與輔助功能編織在一起。
execution(* com.hjsy.service.ProductService.*(..)) 
這表示對滿足如下條件的方法呼叫,進行切面操作:
*  返回任意型別
com.hjsy.service.ProductService.*  包名以 com.hjsy.service.ProductService 開頭的類的任意方法

(..) 引數是任意數量和型別

<?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="hjsy" class="com.hjsy.pojo.Category">  
        <property name="name" value="花季歲月" />  
    </bean>  
    <bean name="hjsy1" class="com.hjsy.pojo.Product">  
        <property name="name" value="花季歲月1" />  
        <property name="category" ref="hjsy" /> 
    </bean>  
    <bean name="hjsy2" class="com.hjsy.service.ProductService">
    </bean>
    <bean id="loggerAspect" class="com.hjsy.aspect.LoggerAspect"/>
    <aop:config>
        <aop:pointcut id="loggerCutpoint"
            expression=
            "execution(* com.hjsy.service.ProductService.*(..)) "/>
        <aop:aspect id="logAspect" ref="loggerAspect">
            <aop:around pointcut-ref="loggerCutpoint" method="log"/>
        </aop:aspect>
    </aop:config>
</beans>


6、測試

TestSpring 程式碼沒有發生任何變化,通過配置的方式,把切面和核心業務類編制在了一起。

執行測試,可以發現在編織之後,業務方法執行之前和之後分別會列印日誌


原始碼:連結:https://pan.baidu.com/s/1nwWF4rn 密碼:88qv