aop:基本的概念就是面向切面编程
spring aop的方式: axpactJ
基本的概念:
通知(advice):
通知类型:前置通知(before),后置通知(after),异常通知(after-throwing),环绕通知(around),返回通知(after-returning)
切点(poincut):就是我们在包中要记录的那些方法
切面:就是要处理的那一层
连接点(joinpoincut):就是为切面,切点处理的那个方法
aop在spring框架中的使用:
记录事物:
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 以方法为单位,指定方法应用什么事务属性 isolation:隔离级别 propagation:传播行为 read-only:是否只读 -->
<tx:method name="save*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="false" />
<tx:method name="persist*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="false" />
<tx:method name="update*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="false" />
<tx:method name="modify*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="false" />
<tx:method name="remove*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="false" />
<tx:method name="get*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="true" />
<tx:method name="find*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="true" />
<tx:method name="transfer" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>
<!-- 配置织入 -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="txPc" />
<!-- 配置切面 : 通知+切点 advice-ref:通知的名称 pointcut-ref:切点的名称 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config>
<aop:config>
<aop:aspect ref="aopLog">
<aop:before pointcut="execution(* com.mvc.aop.shop.*.*(..))" method="before"/>
<aop:after pointcut="execution(* com.mvc.aop.shop.*.*(..))" method="after"/>
<aop:after-returning pointcut="execution(* com.mvc.aop.shop.*.*(..))" method="afterReturn"/>
<aop:after-throwing pointcut="execution(* com.mvc.aop.shop.*.*(..))" throwing="ex" method="afterThrow"/>
<!-- <aop:around pointcut="execution(* com.mvc.aop.shop.*.*(..))" method="around"/> -->
</aop:aspect>
</aop:config>