AOP、OOP在字面上雖然非常類似,但卻是面向不同領域的兩種設計思想。OOP(面向對象編程)針對業務處理過程的實體及其屬性和行爲進行抽象封裝,以獲得更加清晰高效的邏輯單元劃分。 而AOP則是針對業務處理過程中的切面進行提取,它所面對的是處理過程中的某個步驟或階段,以獲得邏輯過程中各部分之間低耦合性的隔離效果。這兩種設計思想在目標上有着本質的差異。
舉個簡單的例子,對於“僱員”這樣一個業務實體進行封裝,自然是OOP的任務,我們可以爲其建立一個“Employee”類,並將“僱員”相關的屬性和行爲封裝其中。而用AOP設計思想對“僱員”進行封裝將無從談起。同樣,對於“權限檢查”這一動作片斷進行劃分,則是AOP的目標領域。而通過OOP對一個動作進行封裝,則有點不倫不類。 換而言之,OOP面向名詞領域,AOP面向動詞領域。
總之,AOP可以通過預編譯方式和運行期動態代理實現在不修改源代碼的情況下給程序動態統一添加功能的一種技術。
package spring.aop;
public interface Service {
//一個接口
void add();
void update();
void delete();
}
package spring.aop;
public class ServiceImpl implements Service {
//第一個特實現類
public void add() {
System.out.println("add");
}
public void update() {
System.out.println("udpate");
}
public void delete() {
System.out.println("delete");
}
}
package spring.aop;
public class ServiceImpl2 implements Service {
//第二個實現類
public void add() {
System.out.println("add22");
}
public void update() {
System.out.println("update222");
}
public void delete() {
System.out.println("delete222");
}
}
package aopxml; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; //切面 也就是增強的代碼 public class MyAspect implements MethodInterceptor{ @Override public Object invoke(MethodInvocation mi) throws Throwable { System.out.print("----------前代碼");//增強的代碼 Object obj = mi.proceed(); System.out.println("----------後代碼"); return obj; } }
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 目標類 --> <bean id="service1" class="spring.aop.ServiceImpl"></bean> <bean id="service2" class="spring.aop.ServiceImpl2"></bean> <!-- 切面 --> <bean id="MyAspectId" class="aopxml.MyAspect"></bean> <aop:config> <aop:pointcut expression="execution(* spring.aop.Service*.add*(..))" <!-- 這裏就是篩選過濾的地方,切入點表達式,具體請看上一篇 http://blog.csdn.net/qq_39654446/article/details/77886120 --> id="myPointCut"/> <aop:advisor advice-ref="MyAspectId" pointcut-ref="myPointCut"/> </aop:config> </beans>
package aopxml;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;//切面 也就是增強的代碼public class MyAspect implements MethodInterceptor{@Overridepublic Object invoke(MethodInvocation mi) throws Throwable {System.out.print("----------前代碼");//增強的代碼Object obj = mi.proceed();System.out.println("----------後代碼");return obj;}}
package aopxml;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.aop.Service;
public class Teat {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("aopxml/springaop.xml");
Service service = (Service) ac.getBean("service2");
service.add();
System.out.println("-----------");
service.delete();
System.out.println("--------------");
service.update();
System.out.println("---------------");
Service service1 = (Service) ac.getBean("service1");
service1.add();
System.out.println("-------------------");
service1.delete();
System.out.println("------------------");
service1.update();
}
}