aop注解 事例

spring.xml中aop的配置

 1   <!-- 
 2          aop的配置
 3       -->
 4       <aop:config>
 5           <!-- 
 6                切入点表达式
 7            -->
 8           <aop:pointcut expression="execution(* cn.itcast.spring.aop.sh.PersonDaoImpl.*(..))" id="perform"/>
 9           <aop:aspect ref="myTransaction">
10           <!-- 
11               <aop:before method="beginTransaction" pointcut-ref="perform"/>
12               <aop:after-returning method="commit" pointcut-ref="perform" returning="var"/>
13            -->
14               <aop:after method="finallyMethod" pointcut-ref="perform"/>
15               <aop:after-throwing method="throwingMethod" pointcut-ref="perform" throwing="ex"/>
16               <aop:around method="aroundMethod" pointcut-ref="perform"/>
17           </aop:aspect>
18       </aop:config>

 自定义的切面

注解形式的aop 写着 切面上

 1 package cn.itcast.spring.aop.annotation.sh;
 2 
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.ProceedingJoinPoint;
 5 import org.aspectj.lang.annotation.AfterReturning;
 6 import org.aspectj.lang.annotation.Aspect;
 7 import org.aspectj.lang.annotation.Before;
 8 import org.aspectj.lang.annotation.Pointcut;
 9 import org.hibernate.Transaction;
10 import org.springframework.stereotype.Component;
11 
12 
13 /**
14  * @Aspect
15  * ==
16  * <aop:config> 
17  *      <aop:pointcut
18  *         expression=
19  *     "execution(* cn.itcast.spring.aop.annotation.sh.PersonDaoImpl.*(..))"
20  *     id="aa()"/>
21  * </aop:config>
22  * @author Think
23  *
24  */
25 @Component("myTransaction")
26 @Aspect
27 public class MyTransaction extends HibernateUtils{
28     private Transaction transaction;
29     
30     @Pointcut("execution(* cn.itcast.spring.aop.annotation.sh.PersonDaoImpl.*(..))")
31     private void aa(){}//方法签名  返回值必须是void  方法的修饰符最好是private ,aa是随便定义的 做类比
32     
33     @Before("aa()")
34     public void beginTransaction(JoinPoint joinpoint){
35         this.transaction = sessionFactory.getCurrentSession().beginTransaction();
36     }
37     
38     @AfterReturning(value="aa()",returning="val")
39     public void commit(Object val){
40         this.transaction.commit();
41     }
42     
43 }

 然后在spring.xml中引入

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 8                          http://www.springframework.org/schema/context
 9                          http://www.springframework.org/schema/context/spring-context-3.0.xsd
10                          http://www.springframework.org/schema/tx
11                          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
12                          http://www.springframework.org/schema/aop 
13                          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
14 
15 
16 <!--      扫描注解bean -->
17      <context:component-scan base-package="cn.us.aspect"/>
18 <!--      开启切面代理 使得spring认识 @Aspect -->
19      <aop:aspectj-autoproxy/>
原文地址:https://www.cnblogs.com/friends-wf/p/3826893.html