1.需要插入到目标方法各个位置的方法(即切面)
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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;
public class Audience {
public void takeSeats(JoinPoint joinPoint) {
//获取目标类名
String classname=joinPoint.getTarget().getClass().getName();
//获取目标类方法名
String methodname=joinPoint.getSignature().getName();
StringBuilder log=new StringBuilder();
log.append("before:")
.append(classname)
.append("@")
.append(methodname)
.append(",Param:");
//获取目标类方法的参数
Object[] args=joinPoint.getArgs();
int i=args.length;
for(Object arg : args)
{
if(i==1)
log.append(arg.toString());
else {
log.append(arg.toString()+",");
}
}
System.out.println(log);
System.out.println("----表演马上开始,请先占位----");
}
@Before("execution(* com.juggler.Performer.perform(..))")
public void turnOffCellPhones()
{
System.out.println("-----开始之前,请关机---");
}
@AfterReturning("execution(* com.juggler.Performer.perform(..))")
public void applaud() {
System.out.println("----表演的不错,请鼓掌---");
}
@After("execution(* com.juggler.Performer.perform(..))")
public void turnOnCellPhones() {
System.out.println("---演出结束,可以开机---");
}
@AfterThrowing("execution(* com.juggler.Performer.perform(..))")
public void demandRefund() {
System.out.println("---表演的不好,退钱---");
}
@Around("execution(* com.juggler.Performer.perform(..))")
public void watchPerformance(ProceedingJoinPoint joinPoint) {
try {
System.out.println("=====环绕开始=====");
long start=System.currentTimeMillis();
System.out.println("签名:"+joinPoint.getSignature());
joinPoint.proceed();
long end=System.currentTimeMillis();
System.out.println("共耗时="+(end-start)+"毫秒");
System.out.println("===环绕结束===");
} catch (Throwable e) {
System.out.println("切入失败");
e.printStackTrace();
}
}
}
2.目标方法的接口类
}
3.目标方法的实现类
@Override
public void perform(int i) {
System.out.println("----Juggler的perform方法被调用----");
}
}
4.配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<bean id="juggler" class="com.juggler.Juggler"/>
<bean id="audience" class="com.audience.Audience"/>
<!-- Spring默认不支持@AspectJ风格的切面声明,为了支持需要使用如下配置:
<aop:aspectj-autoproxy/>
这样Spring就能发现@AspectJ风格的切面并且将切面应用到目标对象。 -->
<aop:aspectj-autoproxy/>
</beans>
5.测试类
import javax.faces.application.Application;
import org.springframework.context.support.ClassPathXmlApplicationContext;
ApplicationContext context=new ClassPathXmlApplicationContext("config.xml");
//此时必须是接口类的对象
Performer performer=(Performer) context.getBean("juggler");
System.out.println("----Service层的日志文件的输出如下----");
performer.perform(2);
}
}