`
dyy_gusi
  • 浏览: 207384 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

spring中aop的简单使用(Annotation方式)

阅读更多

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.仅供自己学习使用,不喜勿喷。

   

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics