Spring学习笔记(二)----Spring AOP配置与应用

来源:互联网 发布:淘宝网上兼职靠谱吗 编辑:程序博客网 时间:2024/04/20 11:23

一、什么是AOP

1.     面向切面编程Aspect-Oriented-Programming

      a)     是对面向对象的思维方式的有力补充

2.     参考Spring_1400_AOP_Introduction

3.     好处:可以动态的添加和删除在切面上的逻辑而不影响原来的执行代码 
    a)     Filter

    b)     Struts2的interceptor

4.     概念:

    a)     JoinPoint

    b)     PointCut

    c)     Aspect(切面)

    d)     Advice

    e)     Target

    f)      Weave

二、Spring AOP配置与应用

1.     两种方式:

    a)     使用Annotation

    b)     使用xml

2.     Annotation

    a)     加上对应的xsd文件spring-aop.xsd

    b)     beans.xml <aop:aspectj-autoproxy/>

     c)     此时就可以解析对应的Annotation了

    d)     建立我们的拦截类

    e)     用@Aspect注解这个类

    f)      建立处理方法

   g)     用@Before来注解方法

   h)     写明白切入点(execution …….)

   i)       让spring对我们的拦截器类进行管理@Component

3.     常见的Annotation:

    a)     @Pointcut

    b)     @Before

    c)     @AfterReturning

    d)     @AfterThrowing

    e)     @After

    f)      @Around

@Aspect@ComponentpublicclassLogInterceptor {    @Pointcut("execution(public* com.bjsxt.service..*.add(..))")    publicvoidmyMethod(){};       @Before("myMethod()")    publicvoidbefore() {       System.out.println("methodbefore");    }       @AfterReturning("myMethod()")    publicvoid afterReturning(){       System.out.println("methodafter returning");    }    @Around("myMethod()")    publicvoidaroundMethod(ProceedingJoinPoint pjp)throwsThrowable {       System.out.println("methodaround start");       pjp.proceed();       System.out.println("methodaround end");    }} 


4.     织入点语法

    a)     void !void

    b)     参考文档(* ..)

5.     xml配置AOP

    a)     把interceptor对象初始化

    b)     <aop:config

     <aop:aspect …..

     <aop:pointcut

     <aop:before

 


原创粉丝点击