一、aop简单测试例子代码
public interface IHelloWord {
public String sayHello(String message);
public void sysHi();
}
public class HelloWord implements IHelloWord {
public String sayHello(String message) {
System.out.println("执行sayHello()方法");
return message;
}
public void sysHi() {
System.out.println("执行sysHi()方法");
}
}
package com.mvc.aop.test;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
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;
import org.springframework.stereotype.Component;
/**
* 类名: HelloWordAspect
* 描述:
* @author
* @date Feb 1, 2013 10:08:32 AM
*
*
*/
@Component("helloWordAspect")
@Aspect
public class HelloWordAspect {
private static Logger log = Logger.getLogger(HelloWordAspect.class);
//声明一个切入点第一个*号返回类型,test后面..代表包及下面子包【不写就只代表当前包】,*所有方法,(..)方法的参数
@Before(value = "execution(* com.mvc.aop.test.HelloWord.*(..))")
public void beforeSayHello(JoinPoint joinPoint) {
System.out.println("Before :");// + joinPoint.getArgs()[0]
}
@After(value = "execution(* com.mvc.aop.test..*(..)) && args(message)")
public void afterSayHello(String message) {
System.out.println("After : " + message);
}
@Around(value = "execution(* com.mvc.aop.test..*(..))")
public void aroundSayHello(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Around Before !! ");
joinPoint.proceed();
System.out.println("Around After !! ");
}
@AfterThrowing(value = "execution(* com.mvc.aop.test..*(..))", throwing = "ex")
public void afterThrowingSayHello(Exception ex) {
System.out.println("After Throwing : " + ex.getMessage());
}
// @AfterReturning(value = "execution(* com.mvc.aop.test..*(..))", returning = "message")
// public void afterReturningSayHello(String message) {
// System.out.println("After Returning : " + message);
// }
@AfterReturning(value = "execution(* com.mvc.aop.test..*(..))")
public void afterReturning(JoinPoint joinPoint) {
System.out.println("After Returning : " );
Signature signature = joinPoint.getSignature();
log.debug("DeclaringType:" + signature.getDeclaringType());
log.debug("DeclaringTypeName:" + signature.getDeclaringTypeName());
log.debug("Modifiers:" + signature.getModifiers());
log.debug("Name:" + signature.getName());
log.debug("LongString:" + signature.toLongString());
log.debug("ShortString:" + signature.toShortString());
for (int i = 0; i < joinPoint.getArgs().length; i++) {
Object arg = joinPoint.getArgs()[i];
if(null != arg) {
log.debug("Args:" + arg.toString());
}
}
}
}
package com.mvc.aop.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 类名: HelloWordTest
* 描述:
* @author
* @date Feb 1, 2013 10:24:17 AM
*
*
*/
public class HelloWordTest {
@Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-servlet.xml");
IHelloWord helloWord = (IHelloWord) ctx.getBean("helloWord");
helloWord.sayHello("Hello Word China!!");
//helloWord.sysHi();
}
}
spring-servlet.xml描述
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:annotation-config/>
<!-- aop动态代理 -->
<aop:aspectj-autoproxy/>
<!-- 自动扫描bean,把作了注解的类转换为bean -->
<context:component-scan base-package="com.mvc" />
执行结果:
二 说明
1.pointcut语法
execution ——for matching method execution join points
within —— 指定连接点所在的Java类型。
this ——bean reference (Spring AOP proxy) is an instance of the given type
args —— 指定传入到连接点的参数
@target —— the class of the executing object has an annotation of the given type
@args —— the runtime type of the actual arguments passed have annotations of the given type(s)
@within —— limits matching to join points within types that have the given annotation
@annotation —— the subject of the join point (method being executed in Spring AOP) has the given annotation
组合pointcut表达式
可以使用 && 、|| 、! 组合pointcut表达式。
execution格式
execution(
可见性(可选) —— 使用public、protected、private指定可见性。也可以为*,表示任意可见性
返回值类型(必须) —— 指定返回值类型
声明类型(可选)——
方法名(参数模式)(必须) —— 方法名称,和方法接收的参数
异常模式(可选) —— 指定方法签名中是否存在异常类型
)
在pointcut表达式中可以使用 * 、.. 、 +等通配符。
* :表示若干字符(排除 . 在外)
.. :表示若干字符(包括 . 在内)
+ :表示子类 ,比如Info+ 表示Info类及其子类