<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> ... </beans>
<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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
...
</beans>
<!-- 開啓bean組件掃描 --> <context:component-scan base-package="dulk.learn"></context:component-scan> <!-- 啓用自動代理 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 開啓bean組件掃描 -->
<context:component-scan base-package="dulk.learn"></context:component-scan>
<!-- 啓用自動代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
import org.aspectj.lang.JoinPoint; 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; import org.springframework.stereotype.Component; @Component @Aspect public class Section { @Before("execution(* dulk.learn..*.*(..))") public void doBefore(JoinPoint point) { System.out.println("doBefore"); } @AfterReturning(pointcut = "execution(* dulk.learn..*.*(..))", returning = "ret") public void doAfterReturning(JoinPoint point, Object ret) { System.out.println("doAfterReturning"); System.out.println("The returning obj is " + ret); } @AfterThrowing(pointcut = "execution(* dulk.learn..*.*(..))", throwing = "e") public void doThrow(JoinPoint point, Throwable e) { System.out.println("doThrow"); } @After("execution(* dulk.learn..*.*(..))") public void doAfter() { System.out.println("doAfter"); } @Around("execution(* dulk.learn..*.*(..))") public void doAround(ProceedingJoinPoint point) { System.out.println("doAround-dobefore"); try { Object obj = point.proceed(); System.out.println("doAround-doAfterReturning"); } catch (Throwable throwable) { System.out.println("doAround-doThrow"); } System.out.println("doAround-doAfter"); } }
x
import org.aspectj.lang.JoinPoint;
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;
import org.springframework.stereotype.Component;
public class Section {
"execution(* dulk.learn..*.*(..))") (
public void doBefore(JoinPoint point) {
System.out.println("doBefore");
}
pointcut = "execution(* dulk.learn..*.*(..))", returning = "ret") (
public void doAfterReturning(JoinPoint point, Object ret) {
System.out.println("doAfterReturning");
System.out.println("The returning obj is " + ret);
}
pointcut = "execution(* dulk.learn..*.*(..))", throwing = "e") (
public void doThrow(JoinPoint point, Throwable e) {
System.out.println("doThrow");
}
"execution(* dulk.learn..*.*(..))") (
public void doAfter() {
System.out.println("doAfter");
}
"execution(* dulk.learn..*.*(..))") (
public void doAround(ProceedingJoinPoint point) {
System.out.println("doAround-dobefore");
try {
Object obj = point.proceed();
System.out.println("doAround-doAfterReturning");
} catch (Throwable throwable) {
System.out.println("doAround-doThrow");
}
System.out.println("doAround-doAfter");
}
}