在开发中,为了给业务方法中增加日志记录,权限检查,事务控制等功能,此时我们需要去修改业务方法代码,考虑到代码的重用性,我们可以考虑使用 OOP 的继承或组合关系来消除重复,但是无论怎么样,我们都会在业务方法中纵向地增加这些功能方法的调用代码。此时,既不遵循开闭原则,也会为后期系统的维护带来很大的麻烦。(即不管怎样都得修改到原来的代码)为了解决该问题,OOP 思想是不行了,得使用 AOP 思想。
(学习之后的总结)
Spring配置AOP有xml和注解两种方法
xml配置AOP
在AOP包里面新建AOP(事务)对象
public class MyAspect {
public void before(){
System.out.println("前置通知");
}
public void afterReturning(){
System.out.println("后置通知");
}
//环绕通知
public Object around(ProceedingJoinPoint around) throws Throwable {
System.out.println("环绕通知之前");
Object proceed = around.proceed(); //执行目标方法
System.out.println("环绕通知之后");
return proceed; //将目标方法的返回值返回
}
public void after(){
System.out.println("最终通知");
}
public void afterThrowing(){
System.out.println("异常抛出通知");
}
}
在 applicationContext.xml 里面配置。
<!--AOP(事务)对象-->
<bean id="aspect" class="com.bohang.tx.MyAspect" />
<bean id="tx" class="com.bohang.tx.MyTransactionManager" />
<!--具体实现类-->
<bean id="employeeService" class="com.bohang.service.impl.EmployeeServiceImpl" />
<bean id="userService" class="com.bohang.service.impl.UserServiceImpl" />
<!--xml配置AOP CGLIB代理 proxy-target-class="true" -->
<aop:config>
<aop:aspect ref="aspect">
<!--定义切入点 where
id:切点名称
expression:切点表达式
-->
<aop:pointcut id="myPointcut"
expression="execution(* com.bohang.service.impl.*ServiceImpl.*(..))"/>
<!-- 上面切入点切到方法,在方法执行之前,加 MyAspect.before() 方法 -->
<aop:before pointcut-ref="myPointcut" method="before" />
<!--环绕通知-->
<aop:around pointcut-ref="myPointcut" method="around"/>
<!-- 上面切入点切到方法,在方法正常执行完,加 MyAspect.afterReturning() 方法 -->
<aop:after-returning pointcut-ref="myPointcut" method="afterReturning" />
<!-- 上面切入点切到方法,在方法执行抛出异常时,加 MyAspect.afterThrowing() 方法-->
<aop:after-throwing pointcut-ref="myPointcut" method="afterThrowing"/>
<!--方法执行最后,最终通知-->
<aop:after pointcut-ref="myPointcut" method="after" />
</aop:aspect>
</aop:config>
aop配置注意事项
在我们开发中通知顺序应该为:
-
前置通知
-
环绕通知之前
-
目标方法执行
-
环绕通知之后
-
后置通知
-
最终通知
但是如果在applicationContext.xml配置顺序如果不一样则会导致不同的情况,因此必须严格控制在spring配置文件中配置的通知顺序。
注解配置AOP
注解说明
@Aspect 作用:把当前类声明为切面类。贴在方法上(切面)
@Order(10) 作用:表示顺序,越小越早执行
@Pointcut("execution(* com.bohang.service.impl.*ServiceImpl.*(..))") 作用:指定切入点表达式
@Before("txPointCut()") 作用:把当前方法表示为前置通知
@AfterReturning("txPointCut()") 作用:把当前方法表示为正常执行的后置通知
@AfterThrowing("txPointCut()") 作用:把当前方法表示为异常时执行的通知
@After 作用:把当前方法表示为最终通知,不管报不报异常都会执行
@Around 作用:把当前方法表示为环绕通知
applicationContext.xml 文件配置
<!--配置业务对象-->
<context:component-scan base-package="com.bohang" />
<!--配置AOP注解解析器 CGLIB proxy-target-class="true"-->
<aop:aspectj-autoproxy />
切面类
@Component
@Aspect
public class AnnoMyAspect {
@Pointcut("execution(* com.bohang.service.impl.*ServiceImpl.*(..))")
public void AnnoMyAspect(){}
@Before("AnnoMyAspect()") //引用切点
public void before(){
System.out.println("前置通知");
}
@AfterReturning("AnnoMyAspect()")
public void afterReturning(){
System.out.println("后置通知");
}
//环绕通知
@Around("AnnoMyAspect()")
public Object around(ProceedingJoinPoint around) throws Throwable {
System.out.println("环绕通知之前");
//执行目标方法
Object proceed = around.proceed();
System.out.println("环绕通知之后");
//将目标方法的返回值返回
return proceed;
}
@After("AnnoMyAspect()")
public void after(){
System.out.println("最终通知");
}
@AfterThrowing("AnnoMyAspect()")
public void afterThrowing(){
System.out.println("异常抛出通知");
}
}