Spring AOP之基于注解的AOP

基于注解AOP步骤

  1. 将目标类和切面类加到IOC容器中。@Component
  2. 告诉Spring哪个是切面类。@Aspect
  3. 在切面类中使用五个通知注解中的这些通知方法都何时运行
  4. 开启基于AOP注解功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<bean id="mathCalculator" class="xyz.lyhcc.calculate.MathCalculator"></bean>
<bean id="loggerUtils" class="xyz.lyhcc.logger.LoggerUtils"></bean>
<bean id="validAspect" class="xyz.lyhcc.logger.ValidAspect"></bean>


<aop:config>
<aop:pointcut expression="execution(* xyz.lyhcc.calculate.MathCalculator.*(int,int))" id="glabalPointcut"/>
<aop:aspect ref="loggerUtils">
<aop:before method="logStart" pointcut-ref="glabalPointcut"/>
<aop:after-returning method="logFinished" pointcut-ref="glabalPointcut" returning="result"/>
<aop:after-throwing method="logError" pointcut-ref="glabalPointcut" throwing="exception"/>
<aop:after method="logFinal" pointcut-ref="glabalPointcut"/>
<aop:around method="aroundMethod" pointcut-ref="glabalPointcut"/>
</aop:aspect>

<aop:aspect ref="validAspect">
<aop:before method="logStart" pointcut-ref="glabalPointcut"/>
<aop:after-returning method="logFinished" pointcut-ref="glabalPointcut" returning="result"/>
<aop:after-throwing method="logError" pointcut-ref="glabalPointcut" throwing="exception"/>
<aop:after method="logFinal" pointcut-ref="glabalPointcut"/>

</aop:aspect>

</aop:config>

脚下留心
注解: 快速方便
配置: 功能完善
重要的用配置,不重要的使用注解


Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×