预编译方式:AspectJ
动态代理方式(JDK动态代理,CGLIB动态代理):Spring AOP,JbossAOP
2、基本概念切面(Aspect):对程序进行切割的类,描述切割的点和切割前后需要执行的动作。
连接点(Joinpoint):符合切入点条件的某个具体的点。
通知(Advice):对应切面中要执行的某个动作。
切入点(Pointcut):切面要切割程序的位置。
引入(Introduction):
目标对象(Target Object):被切割的对象
3、通知的类型前置通知(Before advice):在连接点之前执行的通知。前置通知不能阻止连接点的执行,除非抛出异常。
后置通知(After advice):在连接点之后执行的通知。
返回后通知(After returning advice):在连接点正常完成退出后执行的通知。
异常后通知(After throwing advice):在连接点抛出异常退出后执行的通知。
环绕通知(Around advice):包围连接点的通知,可以在连接点前后执行某些操作。
4、常用切入点表达式执行所有的public方法:
`execution(public * *(..))` 执行所有以set开始的方法:
`execution(* set*(..))` 执行HelloSpringService类中的所有方法:
`execution(* com.learn.service.HelloSpringService.*(..))` 执行service包中的所有方法:
`execution(* com.learn.service..(..))` 执行service包及子包下的所有方法:
`execution(* com.learn.service...(..))` 5、程序示例 xml文件中添加对aop的支持
声明切面,切面中添加切入点和通知
<!--前置通知--> <!--返回后通知--> <!--后置通知--> <!--异常后通知--> <!--无参环绕通知--> <!--带参环绕通知--> 切面类
public class MyAspect {
public void beforeAdvice()
{
System.out.println("beforeAdvice | before advice");
}
public void afterReturning()
{
System.out.println("afterReturning | after returning advice");
}
public void afterAdvice()
{
System.out.println("afterAdvice | after advice");
}
public void afterThrowing()
{
System.out.println("afterThrowing | after throwing advice");
}
public void aroundAdvice(ProceedingJoinPoint pdj) throws Throwable
{
System.out.println("aroundAdvice | 1 around advice");
pdj.proceed();
System.out.println("aroundAdvice | 2 around advice");
}
public void aroundAdvice2(ProceedingJoinPoint pdj, String word) throws Throwable
{
System.out.println("aroundAdvice2 | 1 around advice");
pdj.proceed();
System.out.println("aroundAdvice2 | 2 around advice");
}
} 6、Introductions Introductions为匹配到的Bean类型增加一个父类接口,并在指定的类中增加该接口的实现细节。
xml配置
接口
package learn.interfaces;
public interface Singer {
void sing();
}
实现类
package learn.service;
import learn.interfaces.Singer;
public class SingerImpl implements Singer {
public void sing() {
System.out.println("sing a song!!");
}
}
验证程序
Singer helloService = (Singer) context.getBean("helloService");
helloService.sing(); 输出
sing a song!! 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2157/viewspace-2800716/ ,如需转载,请注明出处,否则将追究法律责任。