spring面向切面编程aop的学习(1)
来源:互联网 发布:电气火灾监控软件 编辑:程序博客网 时间:2024/05/30 04:40
AOP框架是Spring的一个重要组成部分。AOP在能提供企业服务,最重要的是声明式事务管理,通过aop可以对目标方法进行拦截,把想要进行的操作织入到目标方法。常见的对dao层声明式事务,日志管理,安全,权限管理等等。
1.AOP的概念,也是些难以理解痛苦的术语
- 切面(Aspect):一个关注点的模块化。就是值一个类。
- 连接点(Joinpoint):表示一个方法的执行。
- 通知(Advice):在连接点执行前,执行后,调用某个或某些方法。有前置,后置,最终,异常,环绕5中通知。最强大的属于环绕通知,可以控制目标方法的执行。
- 切入点(Pointcut):匹配连接点的断言。就是条件判断,execution(* com.lg.aop.EmployeeDao.* (..))
- 目标对象(Target Object): 被一个或者多个切面所通知的对象。
- 织入(Weaving):形成代理对象的方法的过程。
2.AOP的配置
applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="transaction" class="com.lg.aop.Transaction"></bean> <bean id="employeeDao" class="com.lg.aop.EmployeeImpl"></bean> <bean id="logger" class="com.lg.aop.Logger"></bean> <aop:config> <!-- 切入点 --> <aop:pointcut expression="execution(* com.lg.aop.EmployeeDao.* (..))" id="pt" /> <!-- 定义切面 对切入点进行多个切面操作时候,通知order可以实现调用顺序先后 --> <aop:aspect ref="transaction" order="2"> <aop:before method="beginTransaction" pointcut-ref="pt"/> <aop:after method="commit" pointcut-ref="pt"/> <aop:after-returning method="after" pointcut-ref="pt"/> <aop:after-throwing method="afterThrow" throwing="ex" pointcut-ref="pt"/> <aop:around method="around" pointcut-ref="pt"/> </aop:aspect> <aop:aspect ref="logger" order="1"> <aop:before method="log" pointcut-ref="pt"/> </aop:aspect> </aop:config></beans>
EmployeeDao.java
package com.lg.aop;public interface EmployeeDao {void saveEmployee();}
EmployeeImpl
package com.lg.aop;public class EmployeeImpl implements EmployeeDao {@Overridepublic void saveEmployee() {System.out.println("saveEmployee");}}
Logger
package com.lg.aop;public class Logger {public void log() {System.out.println("logger...");}}
Transaction
package com.lg.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;public class Transaction {public void beginTransaction(JoinPoint joinPoint) {//System.out.println("连接点:"+joinPoint.getSignature().getName());//System.out.println("目标类:"+joinPoint.getTarget().getClass());System.out.println("beginTransaction");}public void commit() {System.out.println("endTransaction");}public void afterThrow(JoinPoint joinPoint,Throwable ex) {System.out.println("Throw.."+ex.getMessage());}public void after() {System.out.println("finally..");}public void around (ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("aa");joinPoint.proceed();}}
测试类:
package com.lg.aop;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class EmployeeTest {@Testpublic void aop() {ApplicationContext context=new ClassPathXmlApplicationContext("app*.xml");EmployeeDao employeeDao=(EmployeeDao)context.getBean("employeeDao");employeeDao.saveEmployee();}}
结果:
logger...
beginTransaction
aa
saveEmployee
endTransaction
finally..
阅读全文
0 0
- spring面向切面编程aop的学习(1)
- Spring 的面向切面编程(AOP)
- Spring学习(四)AOP-面向切面切面的编程技术
- [Spring]面向切面编程AOP【学习笔记】
- Spring学习笔记(AOP面向切面编程)
- Spring学习(六)-面向切面编程(AOP)
- Spring学习(8)--AOP(面向切面编程)基础
- spring学习(二)AOP面向切面编程
- Spring学习笔记(三)--面向切面编程AoP
- Spring学习三(AOP面向切面编程)
- Spring 的AOP(面向切面编程)
- spring的AOP面向切面编程
- spring 面向切面编程AOP的原理
- 关于Spring的AOP面向切面编程
- Spring的AOP-面向切面编程
- Spring的AOP面向切面编程
- spring的AOP--面向切面编程
- AOP-Spring的面向切面编程
- VIM显示如何行号
- sublime使用技巧
- POJ2251-Dungeon Master
- Excel在统计分析中的应用—第九章—非参数检验-独立性检验
- 洛谷1090 合并果子
- spring面向切面编程aop的学习(1)
- ubuntu 解决“无法获得锁 /var/lib/dpkg/lock -open (11:资源暂时不可用)”的办法
- Wannafly挑战赛5 D题题解 数学
- ajax使用函数中的this变量问题
- mybatis系列六:使用getMapper方式实现面向接口的编程
- 二叉树的创建、先序、中序、后序遍历
- Spring整合ActiveMQ
- 求下一个质数
- 并查集模板