package com.sjf.bean;
/**
* 歌手实体类
* @author sjf0115
*
*/
public class Singer {
public void perform() {
System.out.println("正在上演个人演唱会... ");
}
}
package com.sjf.bean;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 观众实体类(注解为切面)
* @author sjf0115
*
*/
@Aspect
public class Audience {
// 定义切点
@Pointcut("execution(* com.sjf.bean.Singer.perform(..))")
public void SingerPerform(){
// 空方法
}
// 表演之前
@Before("SingerPerform()")
public void takeSeats(){
System.out.println("the audience is taking their seats...");
}
// 表演成功之后
@AfterReturning("SingerPerform()")
public void applaud(){
System.out.println("very good, clap clap clap...");
}
// 表演失败之后
@AfterThrowing("SingerPerform()")
public void demandRefund(){
System.out.println("very bad, We want our money back...");
}
}
除了注解和无操作的SingerPerform()方法,Audience类在实现上并没有任何改变,Audience类仍然是一个简单的Java对象,能够像以前一样使用(在Spring中使用Bean进行配置)。 |
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id = "singer" class = "com.sjf.bean.Singer">
</bean>
<bean id = "audience" class = "com.sjf.bean.Audience">
</bean>
<aop:aspectj-autoproxy/>
</beans>
the audience is taking their seats...
正在上演个人演唱会...
very good, clap clap clap... |
<aop:aspect>元素和@AspectJ注解都是把一个POJO转变为一个切面的有效方式。但是<aop:aspect>相对@AspectJ的一个明显优势是:不需要实现切面功能的代码(本例中是Audience类代码)。通过@AspectJ,我们必须标注类和方法,它需要有源码 |
@Around("SingerPerform()")
public void PerformTime(ProceedingJoinPoint joinPoint){
// 演出之前
System.out.println("the audience is taking their seats...");
try {
long start = System.currentTimeMillis();
// 执行演出操作
joinPoint.proceed();
long end = System.currentTimeMillis();
// 演出成功
System.out.println("very good, clap clap clap...");
System.out.println("该演出共需要 "+(end - start) + " milliseconds");
} catch (Throwable e) {
// 演出失败
System.out.println("very bad, We want our money back...");
e.printStackTrace();
}
}
package com.sjf.bean;
/**
* 歌手实体类
* @author sjf0115
*
*/
public class Singer {
public void perform(String song) {
System.out.println("正在上演个人演唱会... " + song);
}
}
package com.sjf.bean;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 主办方实体类
* @author sjf0115
*
*/
@Aspect
public class Organizers {
// 定义切点
@Pointcut("execution(* com.sjf.bean.Singer.perform(String)) and args(song)")
public void SingerPerform(){
//
}
// 表演之前
@Before("SingerPerform() and args(song)")
public void BeforeSong(String song){
System.out.println("演唱会马上就开始了,演唱歌曲为 " + song);
}
}
// 定义切点
@Pointcut("execution(* com.sjf.bean.Singer.perform(String)) and args(song)")
public void SingerPerform(String song){
//
}
// 表演之前
@Before("SingerPerform(song)")
public void BeforeSong(String song){
System.out.println("演唱会马上就开始了,演唱歌曲为 " + song);
}