• 【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();
         }
    }



  • 相关阅读:
    ld: cannot find lXXX" 如lpthread lgomp
    Glib交叉编译:g__cancellable_lock undeclared!&HEADER/C_IN undeclared!&undefined reference to "localeconv"
    Android_清除/更新Bundle中的数据(不finish() Activity的情况下)
    读Kernel感悟Linux内核启动从hello world说起
    细数二十世纪最伟大的十大算法
    error: *** No iconv() implementation found in C library & libiconv 交叉编译 失败编译
    gnulib+glib+glibc+libc的不同转
    [Android] 以singleInstance模式加载的Activity怎么接收以Bundle方式传递过来的参数 By onNewIntent() but not onResum
    Glib在armlinux下的交叉编译
    python 笔记
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5319448.html
Copyright © 2020-2023  润新知