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


【Spring五】AOP之使用注解配置的更多相关文章

  1. 黑马Spring学习 AOP XML和注解配置 5种通知 切点切面通知织入

    业务类 package cn.itcast.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoin ...

  2. Spring的AOP配置文件和注解实例解析

    1.1           Spring的AOP配置文件和注解实例解析 AOP它利用一种称为"横切"的技术,将那些与核心业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减 ...

  3. MongoDB和Java(5):Spring Data整合MongoDB(注解配置)

    最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...

  4. Spring(十五):通过注解配置 Bean

    在ClassPath中扫描组件 1)组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件: 2)特定组件包含: --- @C ...

  5. Spring(五)AOP简述

    一.AOP简述 AOP全称是:aspect-oriented programming,它是面向切面编号的思想核心, AOP和OOP既面向对象的编程语言,不相冲突,它们是两个相辅相成的设计模式型 AOP ...

  6. 深入学习Spring框架(二)- 注解配置

    1.为什么要学习Spring的注解配置? 基于注解配置的方式也已经逐渐代替xml.所以我们必须要掌握使用注解的方式配置Spring. 关于实际的开发中到底使用xml还是注解,每家公司有着不同的使用习惯 ...

  7. Spring IOC机制之使用注解配置bean

    一. 通过注解配置bean 1.1       概述 相对于XML方式而言,通过注解的方式配置bean更加简洁和优雅,而且和MVC组件化开发的理念十分契合,是开发中常用的使用方式. 1.2       ...

  8. spring aop自动代理注解配置之二

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. spring aop自动代理注解配置之一

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

随机推荐

  1. phpmyadmin数据导入最大限制的解决方法

    mysql导入文件最大限制更改解决方法:phpmyadmin库导入出错:You probably tried to upload too large file. Please refer to doc ...

  2. opencart 百度登录和百度钱包支付插件 响应式适应pc/mobile

    OpenCart(http://www.opencart.com/,http://www.opencartchina.com/)是国外著名的开源电子商务系统, 优势在于前台界面的设计非常适合欧美购物者 ...

  3. 一些static_cast const_cast

    static_cast:干杂活的,那三个都有各自的专有用途,那三个不做的都由这个转型符来做,只要它能做的,用C语法的强制类型转换运算符也一定能够完成:但话又说回来了,C强制类型转换能做的,它可不一定都 ...

  4. POJ 3041 Asteroids 最小点覆盖 == 二分图的最大匹配

    Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...

  5. hexdump——Linux系统的二进制文件查看工具

    hexdump是Linux下的一个二进制文件查看工具,可以将二进制文件转换为ASCII.10进制.16进制或8进制进行查看. 首先我们准备一个测试用的文件test,十六进制如下: 00 01 02 0 ...

  6. [BZOJ 2594] [Wc2006]水管局长数据加强版 【LCT】

    题目链接:BZOJ - 2594 题目分析 这道题如果没有删边的操作,那么就是 NOIP2013 货车运输,求两点之间的一条路径,使得边权最大的边的边权尽量小. 那么,这条路径就是最小生成树上这两点之 ...

  7. JPA2.1 中三个提升应用性能的新功能

    经常在网上看到开发者们抱怨 JPA 性能低下的帖子或文章,但如果仔细查看这些性能问题,常会发现导致问题的根本原因大致包括以下几个: 使用过多的 SQL 查询从数据库中获取所需的实体信息,即我们常说的n ...

  8. RESTful, 说说 http 的 patch method

    最早的时候,我们只需要 GET 和 POST 方法,POST 方法的引入也只是为了消除 URL 过长,参数隐藏,上传文件的问题,完全和语义无关.接触到 RESTful 之后,我们开始思考 GET 和 ...

  9. 【HDOJ】1325 Is It A Tree?

    并查集.需要考虑入度. #include <stdio.h> #include <string.h> #define MAXNUM 10005 int bin[MAXNUM]; ...

  10. 【转】MFC获取程序目录路径方法

    原文网址:http://yeahyuanqing.blog.163.com/blog/static/118025091201149480818/ MFC获得当前应用程序目录的GetCurrentDir ...