Spring中AOP开发,使用Annotation注解
1.先加入依赖的jar包
aspectjart-*.jar
aspectjweaver-*jar
cglib-nodep-*.jar
2.修改配置文件中加入
加入新的命名空间:xmlns:aop="http://www.springframework.org/schema/aop"
加入新的约束文件:http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
加入注册处理器配置:<aop:aspectj-autoproxy/>
完整的配置文件头:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>//这一句是让spring配置文件中打开AOP的注解支持 <!--其他bean的定义--> </beans>
3.建立一个拦截器类,并且使用给这个拦截器类标记上相应的注解
@Aspect // @Aspect这表示是一个切面类,有这个注解以后,spring就可以知道这是一个切面类 public class InterceptorAnnotation { @Pointcut(value = "execution(* com.spring.dao.impl.UserDaoImpl.*(..))") public void anyMethod() { // 这只是定义一个切入点,方便各种通知的引用,这个方法是没有实际意义的 } // 前置通知,在目标方法执行之前操作,可以传递参数进来 @Before(value = "anyMethod()&&args(user)") public void before(User user) { // 在这里使用的实参需要和上面注解中给的参数的名称一致 // 然后就可以在这个地方进行记录日志,权限操作等 System.out.println("---------" + user.getId()); System.out.println("before前置通知annotation"); } // 运行玩的后置通知,在目标方法正确执行完成后执行 @AfterReturning(pointcut = "anyMethod()") public void afterReturning() { // 同样在这个地方可以记录日志等等。注意是正确执行完成后再回到这里来 System.out.println("afterReturning后置通知Annotation"); } // 运行异常通知,在目标方法执行过程中如果抛出异常后执行 @AfterThrowing(pointcut = "anyMethod()", throwing = "e") public void afterThrowing(Exception e) { // 当抛出异常以后,可以在这里记录 System.out.println(e.getMessage()); System.out.println("afterThrowing异常通知Annotation"); } // 最终通知,无论运行成功还是抛出异常,最后都会执行的 @After("anyMethod()") public void after() { System.out.println("after最终通知Annotation"); } // 环绕通知,在目标方法执行前后都可以执行 @Around(value = "anyMethod()") public void around(ProceedingJoinPoint pjp) throws Throwable { // 在目标执行前执行 System.out.println("around前置通知Annotation"); pjp.proceed();// 这个地方会代理执行目标方法 // 在目标执行后执行 System.out.println("around后置通知Annotation"); } } //这里面涉及到几个概念,在后面解释
4.将拦截器类交给spring容器管理
在xml配置文件中加入bean:<bean id="interceptorAnnotation" class="com.spring.aop.InterceptorAnnotation"></bean>
注意,虽让已经在拦截器类上有注解定义了。但是还是需要在spring容器中声明并让spring管理这个拦截器bean
5.测试执行
执行对应切入点的的那个类的方法,这个例子中就是执行com.spring.dao.impl.UserDaoImpl类下面的任意的一个方法。
比如执行UserDaoImpl.addUser(user)方法,会输出如下结果
around前置通知Annotation
----------266531660//这是在前置通知中输出的userID
before前置通知annotation
add User[-266531660,a,b] to DB//这是目标方法执行addUser的时候的输出
around后置通知Annotation
after最终通知Annotation
afterReturning后置通知Annotation
//注意,这个输出中没有异常通知的输出,应为在执行目标方法的时候没有发生异常。
6.《Spring参考手册》中定义了以下几个AOP的重要概念,结合以上代码分析如下:
切面(Aspect) :一个关注点的模块化,这个关注点可能会横切多个对象,就是拦截器类
连接点(Joinpoint) :程序执行过程中的某一行为,就是环绕同通中的参数
通知(Advice) :“切面”对于某个“连接点”所产生的动作,就是各种通知对应的方法
切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联,就是之前定义的anyMethod方法
目标对象(Target Object) :被一个或者多个切面所通知的对象。就是UserDaoImpl类下的任意一个方法
AOP代理(AOP Proxy) 在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。
有关代理的详细解释,http://dyygusi.iteye.com/blog/1994843
通知(Advice)类型:
前置通知(Before advice) :@Before
后通知(After advice) :@After
返回后通知(After return advice) :@AfterReturning
环绕通知(Around advice) :@Around
抛出异常后通知(After throwing advice) :@AfgerThrowing
@Pointcut(value = "execution(* com.spring.dao.impl.UserDaoImpl.*(..))")的解释:
定义一个切入点,里面的value是接入点表达式。最常见的切入点表达式就是用execution表示。
第一个*表示返回任何类型的都可以
com.spring .dao.impl.UserDaoImpl表示对应的切入点的类
第二个*表示切入点类下面的所有方法
(..)表示方法的参数是任意的,可以没有,可以有多个,类型也是任意的
官方解释:execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
modifiers-pattern:方法的操作权限
ret-type-pattern:返回值
declaring-type-pattern:方法所在的包
name-pattern:方法名
parm-pattern:参数名
throws-pattern:异常
带?的表示可以去掉,可有可无的。
7.仅供自己学习使用,不喜勿喷。
相关推荐
Spring_Annotation_AOP
NULL 博文链接:https://baobeituping.iteye.com/blog/1201798
spring-aop实践项目,基于annotation方式的面向切面实践小项目
Spring mvc Aop+annotation实现系统日志记录功能实现的jar包asm-3.3.jar ,aspectjrt.jar , aspectjweaver.jar , cglib-nodep-2.1_3.jar , spring-aop.jar
NULL 博文链接:https://bijian1013.iteye.com/blog/2142528
这里zip压缩包囊括了学习Spring过程中用到的所有的jar包; 有: commons-logging-1.2.jar spring-beans-4.0.4.RELEASE.jar spring-context-4.0.4.RELEASE.jar spring-core-4.0.4.RELEASE.jar spring-expression-...
spring中自定义注解(annotation)与AOP中获取注解.通过实例演示自定义注解。
NULL 博文链接:https://tuoxinquyu.iteye.com/blog/1465187
NULL 博文链接:https://tianhei.iteye.com/blog/978969
aop入门。
NULL 博文链接:https://whp0731.iteye.com/blog/356167
自己动手做的一个spring aop 的小demo,主要是annotation标签的配置联系,比较简单.
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/548314
spring mvc aop <context:annotation-config />
主要介绍了Spring用AspectJ开发AOP(基于Annotation),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: ...
在微服务流行的当下,在使用SpringCloud/Springboot框架开发中,AOP使用的非常广泛,尤其是@Aspect注解方式当属最流行的,不止功能强大,性能也很优秀,还很舒心!所以本系列就结合案例详细介绍@Aspect方式的切面的...
SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo