AOP
了解
AOP 面向切面编程,是为了在更改原代码的情况下对原有的业务方法进行扩展,对公用方法进行抽取。
组成
- 切面(Aspect):一个对象(提供扩展方法的对象)。
- 切入点(PointCut): 执行切面内方法的地址。
- 目标(Target):被切入的对象。
- 建议(Advice):扩展方法的方式。
- 连接点(JoinPoint):被切对象的切入匹配点。
举个例子: 厨子需要做饭,因为吃饭人多这个时候需要有一个人帮他传菜。这个传菜员就是切面。他相当于横向的切入了我们原来的业务中。传菜的动作就相当于一个切入点。传菜员相当于在做菜前后进行功能增强。传菜员需要在做菜前后做出动作(前:报菜名,后:传菜)这个前后就是建议。连接点其实相当于对原业务的工作。
实现方式
使用原生AOP的接口
配置:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="bookService" class="com.spring.service.BookServiceImpl"/>
<bean id="log" class="com.spring.aop.Log"/>
<bean id="afterReturnLog" class="com.spring.aop.AfterReturnLog"/>
<aop:config>
<!-- 切入点 要执行的位置-->
<aop:pointcut id="pointcut" expression="execution(* com.spring.service.BookServiceImpl.*(..))"/>
<!-- 切入的方法-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterReturnLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
java:
//根据方法的不同调用的建议也不同 此时是方法前
public class Log implements MethodBeforeAdvice {
/**
*
* @param method 方法
* @param args 参数
* @param target 目标对象
* @throws Throwable
*/
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName());
}
}
// 方法返回后
public class AfterReturnLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"结果是"+returnValue);
}
}
测试类:
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
BookService bookService = (BookService) context.getBean("bookService");
bookService.add();
}
}
使用自定义类实现
<bean id="aopClass" class="com.spring.demo2.AopClass"/>
<aop:config>
<aop:aspect ref="aopClass">
<aop:pointcut id="pointcut" expression="execution(* com.spring.service.BookServiceImpl.*(..))"/>
<!-- 切入的地址-->
<aop:before method="beforeExecute" pointcut-ref="pointcut"/>
<aop:after method="afterExecute" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
注解的方式
配置:
<bean id="aspectLog1" class="com.spring.annotation.AspectLog1"/>
<bean id="aspectLog2" class="com.spring.annotation.AspectLog2"/>
<aop:aspectj-autoproxy/>
java:
@Aspect
public class AspectLog1 {
@Before("execution(* com.spring.service.BookServiceImpl.*(..))")
public void before1(){
System.out.println("运行前1");
}
@Before("execution(* com.spring.service.BookServiceImpl.*(..))")
public void before2(){
System.out.println("运行前2");
}
@After("execution(* com.spring.service.BookServiceImpl.*(..))")
public void after(){
System.out.println("运行后");
}
@AfterReturning("execution(* com.spring.service.BookServiceImpl.*(..))")
public void afterReturning(){
System.out.println("返回结果后");
}
@Around("execution(* com.spring.service.BookServiceImpl.*(..))")
public Object around(ProceedingJoinPoint pj) throws Throwable {
System.out.println("环绕前");
Object result = pj.proceed();
System.out.println("环绕后");
return result;
}
}
AOP 不同advice 执行顺序
- around->before->方法正常执行->around(结束)->after->afterReturning
- around->before->方法异常执行->around(结束)->after->afterThrowing->抛出异常
同一个切面中
如果是同样的advice建议写到一个PointCut中。或者分为两个Aspect
不同切面中
相同的advice,通过@Order注解来判断先后,值越小越靠前。
不同的advice,按照上述顺序执行。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。