AOP是面向对象的思维方式的有力补充。好处:可以动态的添加和删除在切面上的逻辑而不影响原来的执行代码

1.Anotation

a.在命名空间中加入xsd文件spring-aop.xsd

b.在配置文件中写入<aop:aspectj-autoproxy />   aspectj是专门用来实现代理的框架,可以使用aspectj注解的方式定义spring的AOP

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

d.建立自己的拦截类,用@aspect注解这个类

e.建立处理方法,用@Before来注解这个方法,写明白切入点(execution...)

f.让spring对自己的拦截器类进行管理@component

想要在编写配置文件的时候又提示,window--proferences--xmlcatalog--add

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.stereotype.Component;

@Aspect

@Component//将切面类本身初始化

public class LogInterceptor{

  @Before("execution(public void  ........)//切入点语法

  public void beforeMethod(){

    System.out.println("method start");

  }

}

aspect:切面类及其中的切面逻辑

要为没有实现接口的类动态的产生代理需要引入cglib包

 

2.XML

初始化切面类<bean id="logInterceptor" class="com......"></bean>

          <aop:config>

              <aop:pointcut expression="execution(....)" id="servicePointcut" />// 全局的pointcut,即在哪些方法上加切面逻辑

              <aop:aspect id="logAspect" ref="切面类">//声明切面对象

                <aop:before method="before" pointcut-ref="servicePointcut" />//也可以不用pointcut-ref,而直接定义pointcut,直接指定pointcut

              </aop:aspect>

          </aop:cofig>

posted on 2010-06-01 13:20  Earl  阅读(332)  评论(0编辑  收藏  举报