package com.test.dao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAop {
@Test
public void testAopAnno() {
ApplicationContext context =
new ClassPathXmlApplicationContext("bean1.xml");
User user = context.getBean("user", User.class);
user.add();
}
}
package com.test.dao;
import org.springframework.stereotype.Component;
@Component
public class User {
public void add() {
System.out.println("add.......");
}
}
package com.test.dao;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Aspect
@Order(3)
public class UserProxy {
@Pointcut(value = "execution(* com.test.dao.User.add(..))")
public void pointdemo() {
}
@Before(value = "pointdemo()") pointdemo()等价 "execution(* com.test.dao.User.add(..))"
public void before() {
System.out.println("add()执行前,执行.........");
}
@AfterReturning(value = "execution(* com.test.dao.User.add(..))")
public void afterReturning() {
System.out.println("add()执行后,执行==>后置通知.........");
}
@After(value = "execution(* com.test.dao.User.add(..))")
public void after() {
System.out.println("add()执行完,执行==>最终通知.........");
}
@AfterThrowing(value = "execution(* com.test.dao.User.add(..))")
public void afterThrowing() {
System.out.println("add()执行异常,执行.........");
}
@Around(value = "execution(* com.test.dao.User.add(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕之前.........");
proceedingJoinPoint.proceed();
System.out.println("环绕之后.........");
}
}