在XML中配置切面
参见下图
一、声明前置和后置通知
其中展示了两种声明方法
可以直接声明<aop:before pointcut="execution(* SpringStudy.Model.Counter.perform(..))"
method="turnOffCellPhones" />
也可以先声明切点<aop:pointcut id="perform"
expression="execution(* SpringStudy.Model.Counter.perform(..))" />
然后用pointcut-ref指向该切点
<aop:config>
<aop:aspect id="aspect" ref="audience">
<aop:pointcut id="perform"
expression="execution(* SpringStudy.Model.Counter.perform(..))" />
<aop:before pointcut-ref="perform" method="takeSeats" />
<aop:before pointcut="execution(*
SpringStudy.Model.Counter.perform(..))"
method="takeSeats" />
<aop:before pointcut="execution(* SpringStudy.Model.Counter.perform(..))"
method="turnOffCellPhones" />
<aop:after-returning
pointcut="execution(* SpringStudy.Model.Counter.perform(..))"
method="applaud" />
</aop:aspect>
</aop:config>
Counter
package SpringStudy.Model;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class Counter {
public void perform() {
System.out.println("performing");
}
}
Audience
package SpringStudy.Model.Aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
@Component
public class Audience {
public void takeSeats() {
System.out.println("The audiene is taking their seats.");
}
public void turnOffCellPhones() {
System.out.println("The audiene is turning off their cellphones.");
}
public void applaud() {
System.out.println("CLAP");
}
public void demandRefund() {
System.out.println("Boo! we want our money back!");
}
}
测试类
public class SpringTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 读取bean.xml中的内容
Counter c = ctx.getBean("counter", Counter.class);// 创建bean的引用对象
c.perform();
}
}
二、声明环绕通知
<aop:config>
<aop:aspect id="aspect" ref="audienceNew">
<aop:pointcut id="perform"
expression="execution(* SpringStudy.Model.Counter.perform(..))" />
<aop:around pointcut-ref="perform" method="watchPerformance" />
</aop:aspect>
</aop:config>
AudienceNew类,其他类同上
package SpringStudy.Model.Aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
@Component
public class AudienceNew {
public void watchPerformance(ProceedingJoinPoint joinpoint) {
try {
System.out.println("The audiene is taking their seats.");
System.out.println("The audiene is turning off their cellphones.");
long start = System.currentTimeMillis();
joinpoint.proceed();
long end = System.currentTimeMillis();
System.out.println("CLAP");
} catch (Throwable t) {
System.out.println("Boo! we want our money back!");
}
}
}
注解切面
@Aspect标注一个类,表明这个类是一个切面
@Pointcut注解用于定义一个可以在@AspectJ切面内可以重用的切点,注解值是一个AspectJ表达式
@Before前置通知
@AfterReturing后置通知
@AfterThrowing抛异常时调用
@Around环绕通知
package SpringStudy.Model.Aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
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 NewAudience {
@Pointcut("execution(* SpringStudy.Model.Counter.perform(..))")
public void performance() {
}
@Before("performance()")
public void takeSeats() {
System.out.println("The new audiene is taking their seats.");
}
@Before("performance()")
public void turnOffCellPhones() {
System.out.println("The new audiene is turning off their cellphones.");
}
@AfterReturning("performance()")
public void applaud() {
System.out.println("new CLAP");
}
@AfterThrowing("performance()")
public void demandRefund() {
System.out.println("new Boo! we want our money back!");
}
@Around("performance()")
public void watchPerformance(ProceedingJoinPoint joinpoint) {
try {
System.out.println("around The audiene is taking their seats.");
System.out.println("around The audiene is turning off their cellphones.");
long start = System.currentTimeMillis();
joinpoint.proceed();
long end = System.currentTimeMillis();
System.out.println("around CLAP");
} catch (Throwable t) {
System.out.println("around Boo! we want our money back!");
}
}
}