博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Spring五】AOP之使用注解配置
阅读量:7026 次
发布时间:2019-06-28

本文共 2363 字,大约阅读时间需要 7 分钟。

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

转载地址:http://asoxl.baihongyu.com/

你可能感兴趣的文章
生活感悟(2)
查看>>
AngularJS模块加载
查看>>
rails rake 版本问题
查看>>
.net面试题系列文章九(附答案)
查看>>
前端工程师面试题(html+css)
查看>>
Function Queries - Available Functions
查看>>
memcached 启动参数 和 stat 参数详解
查看>>
第六天,箱子
查看>>
为什么会有psd转css?其优点和缺点?
查看>>
JavaScript 延迟加载
查看>>
父亲写的散文诗
查看>>
利用 Webpack 实现小程序多项目管理
查看>>
FFT总结
查看>>
poj There's Treasure Everywhere!
查看>>
笔记:递归列出目录中的文件列表
查看>>
java 反射机制
查看>>
cocos2d-x中通过Jni实现Java与C++的互相调用
查看>>
Windows Sysinternals Suite 正式版
查看>>
Navicat 如何进行表单查看
查看>>
Gearman 安装
查看>>