基于注解的SpringAOP
来源:互联网 发布:js获取多个相同id 编辑:程序博客网 时间:2024/06/05 07:07
一、SpringAOP主要用于日志,权限控制和事务管理中,在做事务时用得更多。
二、SpringAOP的一些概念:
- SpringAOP被称为面向切面编程;
- Joinpoint连接点:在Spring里面它实际就是我们拦截到的方法
- Pointcut切入点:在Spring里它实际上就是我们所要拦截的方法的定义;
- Advice通知:它是我们在连接点也就是拦截到的方法执行前,执行过程中,以及执行后所要做的处理,这些处理被称为通知,它分为前置通知(在方法执行前被执行的处理),后置通知(在方法成功执行后被执行的处理),最终通知(无论方法是否成功执行,都会执行的处理),例外通知(方法执行期间出现异常会被挂靠的处理);
- Target目标对象:它是代理的委托对象;
三、实现SpringAOP还需要引入以下的包:aspectjweaver.jar,aspectjrt.jar,cglib-nodep-2.1_3.jar,基于注解来实现还需要引用common-annotations.jar这个包。
四、实现SpringAOP功能要修改原来的XML文件,引入AOP命名空间:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/>
</beans>
红色部分为我们引入的AOP命名空间,这是必须的。
五、建立切面
- package cn.itcast.service;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.AfterReturning;
- import org.aspectj.lang.annotation.AfterThrowing;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.Pointcut;
- /**
- * 切面
- *
- */
- @Aspect
- public class MyInterceptor {
- @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")//切面表达式,指定要拦截的方法
- private void anyMethod() {}//声明一个切入点,注意它是以方法的形式进行声明的
- @Before("anyMethod() && args(name)")//前置通知,在方法执行前被调用
- public void doAccessCheck(String name) {
- System.out.println("前置通知:"+ name);
- }
- @AfterReturning(pointcut="anyMethod()",returning="result")//后置通知,在方法成功执行后被执行
- public void doAfterReturning(String result) {
- System.out.println("后置通知:"+ result);
- }
- @After("anyMethod()")//最终通知,无论方法是否执行成功都会被执行
- public void doAfter() {
- System.out.println("最终通知");
- }
- @AfterThrowing(pointcut="anyMethod()",throwing="e")//例外通知,在方法抛出异常时才会被执行
- public void doAfterThrowing(Exception e) {
- System.out.println("例外通知:"+ e);
- }
- @Around("anyMethod()")//环绕通知,可能替代上面的所有通知
- public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
- //if(){//判断用户是否在权限
- System.out.println("进入方法");
- Object result = pjp.proceed();//要执行这个方法,拦截到的方法才会被执行
- System.out.println("退出方法");
- //}
- return result;
- }
- }
package cn.itcast.service;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;/** * 切面 * */@Aspectpublic class MyInterceptor {@Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")//切面表达式,指定要拦截的方法private void anyMethod() {}//声明一个切入点,注意它是以方法的形式进行声明的@Before("anyMethod() && args(name)")//前置通知,在方法执行前被调用public void doAccessCheck(String name) {System.out.println("前置通知:"+ name);}@AfterReturning(pointcut="anyMethod()",returning="result")//后置通知,在方法成功执行后被执行public void doAfterReturning(String result) {System.out.println("后置通知:"+ result);}@After("anyMethod()")//最终通知,无论方法是否执行成功都会被执行public void doAfter() {System.out.println("最终通知");}@AfterThrowing(pointcut="anyMethod()",throwing="e")//例外通知,在方法抛出异常时才会被执行public void doAfterThrowing(Exception e) {System.out.println("例外通知:"+ e);}@Around("anyMethod()")//环绕通知,可能替代上面的所有通知public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {//if(){//判断用户是否在权限System.out.println("进入方法");Object result = pjp.proceed();//要执行这个方法,拦截到的方法才会被执行System.out.println("退出方法");//}return result;}}
注意:每一种通知都可以在后面用&&号继续加入拦截条件,例如在上面的例子中在前置通知里面加入了args参数,指定拦截的方法要有一个参数,并且这个参数的值传给了name。在后置通知里面加入了returnning条件,指定方法返回值传给return参数等等。另外要注意的是:这个切面类要交给Spring进行管理,才能实现方法拦截。
- 基于注解的SpringAOP
- 基于注解的SpringAOP Redis缓存技术
- springAop基于注解配置
- springAOP的注解
- SpringAop的注解形式
- springAOP的注解版
- SpringAOP +log4J+注解的 日志管理
- SpringAOP入门学习一:基于注解配置切面( AspectJ)
- 基于springAop的日志管理功能
- 基于SpringAOP的数据库读写分离实现
- 基于SpringAOP的操作日志记录实现
- springAOP (注解方式)
- springAOP 注解实现
- SpringAop事务注解形式
- SpringAOP实现自定义注解
- SpringAop注解实现
- SpringAOP注解应用
- springAOP进行自定义注解,用于方法的处理
- 关于Urb的一些基础认识
- SRM144_Time_200分
- Android中播放即时音效
- opencv实现不同格式图片的转换(jpg<-->bmp)
- Delphi BPL包安装讲解
- 基于注解的SpringAOP
- eclipse/myeclipse注释模板的修改
- 析构函数中抛出的异常
- Java程序发生异常就挂了吗?
- const vs non-const
- JAVA 中abstract和interface的区别
- hadoop1.01+ hbase 0.92+chukwa0.5 安装配置 +问题
- 启动android默认浏览器
- Java设计模式:装饰者模式(Decorator)