【Spring五】AOP之使用注解配置

AOP使用注解配置流程:
1、当spring容器启动时候,
   < context:component- scan base-package= "cn.itheima03.spring.aop.annotation" ></context :component-scan>

2、在上面的包及子包中查询所有的类,按照类扫描注解的机制把类放入到spring容器中

3、 检查是否配置:<aop:aspectj-autoproxy> </aop: aspectj-autoproxy>

4、查找哪些类上加@Aspect注解,会查找加了该注解的所有的方法,看方法上是否有 @Pointcut注解

5、把 @Pointcut的切入点表达式解析出来和spring中的bean进行匹配,如果匹配成功,则创建代理对象

6、 生成的代理对象的方法=通知+ 目标方法

完整代码:
1.配置文件:appliactionContext.xml
< beans  xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"  >
      <!--
          1、把所有的bean放入到spring容器中,启动类扫描机制的注解
          2、启动AOP的注解
      -->
            <context:component-scan base-package="cn.itheima03.spring.aop.annotation" ></context:component-scan>
            <aop:aspectj-autoproxy></ aop:aspectj-autoproxy>
          
</ beans >

2.给相关类配置注解:
//配置注解,需要产生实例
@Repository ( "classesDao"  )
public  class ClassesDaoImpl extends HibernateUtils implements ClassesDao{

      public  String saveClasses(Classes classes) {
            sessionFactory.getCurrentSession().save(classes);
            return "aaaa" ;
     }

      public  List<Classes> getClasses() {
            return sessionFactory .getCurrentSession().createQuery("from Classes").list();
     }

      public  void updateClasses(Classes classes) {
            sessionFactory.getCurrentSession().update(classes);
     }

}
=============================
/**
 *  @Aspect该注解说明该类是一个切面类 ,等效于:
 * <aop:aspect ref="myTransaction">
 *   <aop:pointcut expression="execution(* cn.itheima03.spring.aop.annotation.ClassesDaoImpl.*(..))"
 *         id=" aa()"/>
 * </aop:aspect>
 */
@Component ( "myTransaction"  )
@Aspect
public  class MyTransaction extends HibernateUtils{
      private  Transaction transaction;
      @Pointcut ( "execution(* cn.itheima03.spring.aop.annotation.ClassesDaoImpl.*(..))" )
      private  void aa(){} //方法签名   方法的修饰符最好为private,返回值必须是void
     
      @Before ( "aa()" )
      public  void beginTransaction(){
            this.transaction = sessionFactory.getCurrentSession().beginTransaction();
     }
     
      @AfterReturning (  "aa()")
      public  void commit(){
            this.transaction .commit();
     }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值