使用spring的aop,要么引入aspectj,要么cglib!
<bean id="aopshow" class="com.zc.springAop.AopShow"></bean>
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution(* com.zc.service.*.*(..))" id="mypoint"/>
<aop:advisor advice-ref="aopshow" pointcut-ref="mypoint"/>
</aop:config>
将 aopshow对象配置到spring容器中.
以下为aop:config:
proxy-target-class 属性值被设置为true,那么基于类的代理将起作用(这时需要cglib库)。如果proxy-target-class属值被设置为false或者这个属性被省略,那么标准的JDK 基于接口的代理将起作用。
pointcut:切入点
expression="execution(* com.zc.service.*.*(..))" 匹配的是com.zc.service下的所有的类的方法.
aop:advisor: 放置一个通知和一个切入点的切面
advice: 通知 ,(执行代码或方法)
以下是使用 aspectj ---面向切面框架: 使用JDK5.0注解.
<aop:aspectj-autoproxy>的作用是自动为spring容器中的那些匹配@aspectj 切面的bean创建代理,完成切面织入.
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
package com.zc.springAop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AopTwo {
@Pointcut("execution(* com.zc.service.*.*(..))")
public void a(){
}
@Before(value = "a()")
public void showOne(){
System.out.println("今天星期六.");
}
@After(value = "a()")
public void showTwo(){
System.out.println("继续奋斗.");
}
}