由于在applicationContext.xml中注入bean以及注册通知,比较麻烦,当运用注解的方式来完成十分便捷
首先要在xml中配置自动扫描以及注解支持
<context:component-scan base-package="spring.aop.anno"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
在通知类上加上@Aspect@Component,将他标记为切面的bean
前置通知使用@Before(execution …)
@Before("execution(* spring.aop.anno.*.*(..))")
其中第一个* 代表任意的方法返回值
第二个和第三表示类名以及方法名
…表示参数列表
下面是方法体
@Before("execution(* spring.aop.anno.*.*(..))")
public void logerBefore(JoinPoint jp) {
String methodName = jp.getSignature().getName();
System.out.println("method: " + methodName + "將要被執行!");
Object[] args = jp.getArgs();
for(Object arg : args) {
System.out.println("=======:>"+arg);
}
}
同理
后置通知:
@After("execution(* spring.aop.anno.*.*(..))")
public void logerAfter(JoinPoint jp) {
String methodName = jp.getSignature().getName();
System.out.println("method: " + methodName + "已經被執行!");
Object[] args = jp.getArgs();
for(Object arg : args) {
System.out.println("============:>" + arg);
}
}
环绕通知:
@Around("execution(* spring.aop.anno.*.*(..))")
public Object logerAround(ProceedingJoinPoint pjp) {
String methodName = pjp.getSignature().getName();
System.out.println("環繞通知 =========>method: " + methodName + "將要被執行已經被執行!");
Object[] args = pjp.getArgs();//獲取參數
args[0] = "1";
try {
Object returnValue = pjp.proceed(args);
System.out.println("環繞通知 =========>method: " + methodName + "已經被執行返回值:! " + returnValue);
// returnValue = "hello World!!";
return new BigDecimal("999999999999999");
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
异常通知
@AfterThrowing(value="execution(* spring.aop.anno.*.*(..))",throwing="exception")
public void loggerAfterThrowing(JoinPoint jp, Exception exception) {
System.out.println("後置異常通知:" + exception);
}
值得注意的是如果在注解中只有一个值,它默认赋值给value此时可不写value=
但是有两个或多个值的时候必须加上value=否则就会报错
@AfterThrowing("execution(* spring.aop.anno.*.*(..))",throwing="exception")
Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘bankService’ defined in file [F:\eWorkSpace\spring.aop\target\classes\spring\aop\anno\BankServiceImpl.class]: BeanPostProcessor before instantiation of bean failed; nested exception is java.lang.IllegalStateException: Returning argument name ‘resultValue’ was not bound in advice arguments
可将切面用pointcut注解表示
@Pointcut("execution(* spring.aop.anno.*.*(..))")
public void loggerPointCut() {
}
在返回通知可用它作为参数
后置返回通知
@AfterReturning(pointcut="loggerPointCut()", returning="resultValue")
public void logerAfterReturning(JoinPoint jp, Object resultValue) {
String methodName = jp.getSignature().getName();
System.out.println("後置返回通知 =========>method: " + methodName + "已經被執行!");
System.out.println("後置返回通知 返回值:" + resultValue);
}
以下是main方法与接口和他的实现类
main方法:
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext-anno.xml");
BankService bs = ioc.getBean("bankService",BankService.class);
bs.transfer("a", "b", new BigDecimal("400"));
接口
实现类