Spring框架的AOP

xml配置AOP的步骤:

1,准备了一个实现接口的bean, 还有一个事务的碎片化方法的类;
 2,把这两个类配置到Spring容器中;
 3,配置springAOP
  1)引入aop命名空间;
  2)配置AOP,格式如下
  <aop:config>
      <aop:point-cut expression=""  id="sample" />
      <aop:aspect ref="">
           <aop:before method="" pointcut-ref="sample">
           <aop:after-returning method="" pointcut-ref="sample">
           <aop:after-throwing method="" pointcut-ref="sample">
      </aop:aspect>
  </aop:config>
      1] aop:config: AOP配置
      2] aop:point-cut: AOP切入点
      3] aop:aspect: AOP切面配置
      4] aop:* 都是一个AOP切面

 4,在接口文件中配置一个业务逻辑对象的接口,DI注入,并在test方法中直接调用该接口的具体业务逻辑

 配置AOP的几种方法:

1、最繁琐的:

 1 <aop:config>
 2         <!--配置一堆切面 -->
 3         <aop:aspect ref="tram">
 4             <!--开始事务 -->
 5             <aop:before method="begin" pointcut="execution(* com.gxxy.spring.aop.kp03_xml.*.*(..))"/>
 6             <!--提交事务 -->
 7             <aop:after-returning method="commit" pointcut="execution(* com.gxxy.spring.aop.kp03_xml.*.*(..))"/>
 8             <!--处理异常 -->
 9             <aop:after-throwing method="rollback" pointcut="execution(* com.gxxy.spring.aop.kp03_xml.*.*(..))"/>
10             <!--finally必须输出的 -->
11             <aop:after method="hishfied" pointcut="execution(* com.gxxy.spring.aop.kp03_xml.*.*(..))"/>
12         </aop:aspect>
13     </aop:config>

 

2、简化的:

 1 <aop:config>
 2         <!-- 配置AOP的切入点 -->
 3         <aop:pointcut expression="execution(* com.gxxy.spring.aop.kp03_xml.*.*(..))" id="all"/>
 4         <aop:aspect ref="tram">
 5             <aop:before method="begin" pointcut-ref="all"/>
 6             <aop:after-returning method="commit" pointcut-ref="all"/>
 7             <aop:after-throwing method="rollback" pointcut-ref="all"/>
 8             <aop:after method="hishfied" pointcut-ref="all"/>
 9         </aop:aspect>
10     </aop:config>

 

注意:前面的两种在xml里面配置还需要两个bean:

1 <!-- 配置service的类,也就是实现接口的类,DAO层 -->
2     <bean id="service" class="com.gxxy.spring.aop.kp03_xml.StudentImpl"></bean>
3     <!-- 配置AOP的那个类 -->
4     <bean id="tram" class="com.gxxy.spring.aop.kp03_xml.TransationManager"></bean>

 

3、直接使用注解的:在代理类里面

 1 package com.gxxy.spring.aop.kp04_annotion;
 2 
 3 import org.aspectj.lang.annotation.After;
 4 import org.aspectj.lang.annotation.AfterReturning;
 5 import org.aspectj.lang.annotation.AfterThrowing;
 6 import org.aspectj.lang.annotation.Aspect;
 7 import org.aspectj.lang.annotation.Before;
 8 import org.aspectj.lang.annotation.Pointcut;
 9 import org.springframework.stereotype.Component;
10 
11 @Aspect
12 //相当于<aop:aspect ref="tram">
13 @Component
14 //IoC
15 public class TransationManager {
16     //<aop:pointcut expression="execution(* com.gxxy.spring.aop.kp03_xml.*.*(..))" id="all"/>
17     @Pointcut("execution(* com.gxxy.spring.aop.kp04_annotion.*.*(..))")
18     public void service(){};
19     //@Before("execution(* com.gxxy.spring.aop.kp03_xml.*.*(..))")
20     @Before("service()")
21     public void begin(){
22         System.out.println("TransationManager.begin()");
23     }
24     @AfterReturning("service()")
25     public void commit(){
26         System.out.println("TransationManager.commit()");
27     }
28     @AfterThrowing("service()")
29     public void rollback(){
30         System.out.println("TransationManager.rollback()");
31     }
32     @After("service()")
33     public void hishfied(){
34         System.out.println("TransationManager.hishfied()");
35     }
36 }

 

注意:Pointcut is not well-formed: expecting 'name pattern' at character position
 

配置aop报错:原因是配置切点表达式的时候报错了,

星号后面没有加空格:

<aop:config>

<aop:pointcut id="transactionPointcut" expression="execution(* project.mybatis.service.*.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="omsMTransactionAdvice" />
</aop:config>

其中,切入点表达式的使用规则:

 1、execution(): 表达式主体。

 2、第一个*号:表示返回类型,*号表示所有的类型。

 3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,com.sample.service.impl包、子孙包下所有类的方法。

 4、第二个*号:表示类名,*号表示所有的类。

 5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。

posted @ 2017-03-28 21:18  时间带我去旅行  阅读(269)  评论(0)    收藏  举报