转载于one_piece20的博客,原文地址:http://blog.csdn.net/kuangfengbuyi/article/details/52487657
Spring AOP的几个概念
1.切面(Aspect):切面就是一个关注点的模块化,如事务管理、日志管理、权限管理等;
2.连接点(Joinpoint):程序执行时的某个特定的点,在Spring中就是一个方法的执行;
3.通知(Advice):通知就是在切面的某个连接点上执行的操作,也就是事务管理、日志管理等;
4.切入点(Pointcut):切入点就是描述某一类选定的连接点,也就是指定某一类要织入通知的方法;
5.目标对象(Target):就是被AOP动态代理的目标对象;
借助下图进行理解:
![image](http://t3.qpic.cn/mblogpic/78d5af17c011648b29b2/2000)
先来了解切面编程的使用
前置增强
创建一个前置增强类
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");
}
当然,我们完全可以只定义一个增强类,让它同时实现 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");
}
环绕增强
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");
}
前置增强、后置增强、环绕增强的接口类图:
![2000.jpg]()
转载于one_piece20的博客,原文地址:http://blog.csdn.net/kuangfengbuyi/article/details/52487657