轉載於one_piece20的博客,原文地址:http://blog.csdn.net/kuangfengbuyi/article/details/52487657
Spring AOP的幾個概念
1.切面(Aspect):切面就是一個關注點的模塊化,如事務管理、日誌管理、權限管理等;
2.連接點(Joinpoint):程序執行時的某個特定的點,在Spring中就是一個方法的執行;
3.通知(Advice):通知就是在切面的某個連接點上執行的操作,也就是事務管理、日誌管理等;
4.切入點(Pointcut):切入點就是描述某一類選定的連接點,也就是指定某一類要織入通知的方法;
5.目標對象(Target):就是被AOP動態代理的目標對象;
藉助下圖進行理解:
先來了解切面編程的使用
前置增強
創建一個前置增強類
package cjq.demo.spring.aop;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
/**
* author: cjianquan
* date: 2016/9/9
*/
public class GreetingBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("GreetingBeforeAdvice Before");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
後置增強
package cjq.demo.spring.aop;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
/**
* author: cjianquan
* date: 2016/9/9
*/
public class GreetingAfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object result, Method method, Object[] args, Object target) throws Throwable {
System.out.println("GreetingAfterAdvice After");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
測試代碼:
@Test
public void test_springAop(){
ProxyFactory proxyFactory = new ProxyFactory(); // 創建代理工廠
proxyFactory.setTarget(new GreetingImpl()); // 射入目標類對象
proxyFactory.addAdvice(new GreetingBeforeAdvice()); // 添加前置增強
proxyFactory.addAdvice(new GreetingAfterAdvice()); // 添加後置增強
Greeting greeting = (Greeting) proxyFactory.getProxy(); // 從代理工廠中獲取代理
greeting.sayHello("Jack");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
當然,我們完全可以只定義一個增強類,讓它同時實現 MethodBeforeAdvice 與 AfterReturningAdvice 這兩個接口,如下
package cjq.demo.spring.aop;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
/**
* author: cjianquan
* date: 2016/9/9
*/
public class GreetingBeforeAndAfterAdvice implements MethodBeforeAdvice, AfterReturningAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(" GreetingBeforeAndAfterAdvice Before");
}
@Override
public void afterReturning(Object result, Method method, Object[] args, Object target) throws Throwable {
System.out.println("GreetingBeforeAndAfterAdvice After");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
測試代碼:
@Test
public void test_springAop1(){
ProxyFactory proxyFactory = new ProxyFactory(); // 創建代理工廠
proxyFactory.setTarget(new GreetingImpl()); // 射入目標類對象
proxyFactory.addAdvice(new GreetingBeforeAndAfterAdvice()); // 添加增強
Greeting greeting = (Greeting) proxyFactory.getProxy(); // 從代理工廠中獲取代理
greeting.sayHello("Jack");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
環繞增強
package cjq.demo.spring.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;
/**
* author: cjianquan
* date: 2016/9/9
*/
@Component("greetingAroundAdvice")
public class GreetingAroundAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
before();
Object result = invocation.proceed();
after();
return result;
}
private void before() {
System.out.println("GreetingAroundAdvice Before");
}
private void after() {
System.out.println("GreetingAroundAdvice After");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
測試代碼:
@Test
public void test_greetingAroundAdvice(){
ProxyFactory proxyFactory = new ProxyFactory(); // 創建代理工廠
proxyFactory.setTarget(new GreetingImpl()); // 射入目標類對象
proxyFactory.addAdvice(new GreetingAroundAdvice()); // 添加增強
Greeting greeting = (Greeting) proxyFactory.getProxy(); // 從代理工廠中獲取代理
greeting.sayHello("Jack");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
前置增強、後置增強、環繞增強的接口類圖:
轉載於one_piece20的博客,原文地址:http://blog.csdn.net/kuangfengbuyi/article/details/52487657