基于注解和XML的AOP实现
实现AOP功能的框架主要有Spring AOP和AspectJ,前者只能对方法进行创建joincut,而后者不仅可以对方法,还可以对字段和构造器创建joincut。
一、注解配置AOP。
public class Test { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Aspect1.class); UserService userService = (UserService) applicationContext.getBean("userServiceImpl"); userService.hello("cj"); } }
@Configuration //相当于创建xml配置文件 @EnableAspectJAutoProxy //开启自动代理,默认是基于接口的jdk动态代理 @ComponentScan(basePackages = "com.cj") @Aspect //定义切面 public class Aspect1 { @Pointcut("execution(* com.cj.*.*(..))")//切入点表达式 public void a() {//切入点签名 } @Before("a()") public void b() { System.out.println("Before"); } @AfterReturning("a()") public void c() { System.out.println("AfterReturning"); } @AfterThrowing("a()") public void d() { System.out.println("AfterThrowing"); } @After("a()") public void e() { System.out.println("After"); } @Around("a()") public void f(ProceedingJoinPoint pjp) throws Throwable { System.out.println("Around"); pjp.proceed(); }
执行结果:
Around Before Hello:cj After AfterReturning
从执行结果可以看到,通知的执行顺序:Around->Before->After->AfterReturning
我们可以改下上面配置@EnableAspectJAutoProxy(proxyTargetClass = true),使用cglib代理。
二、XML配置AOP
public class Test { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); UserService userService = (UserService) applicationContext.getBean("userServiceImpl"); userService.hello("cj"); } }
public class Aspect1 { public void before() { System.out.println("Before"); } public void afterReturning() { System.out.println("AfterReturning"); } public void afterThrowing() { System.out.println("AfterThrowing"); } public void after() { System.out.println("After"); } public void around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("Around"); pjp.proceed(); } }
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.cj"/> <bean id="aspect1" class="com.cj.Aspect1"></bean> <!--实例化切面bean--> <aop:config> <aop:aspect ref="aspect1"> <aop:pointcut id="pc" expression="execution(* com.cj.*.*(..))"/> <aop:around method="around" pointcut-ref="pc"></aop:around> <aop:before method="before" pointcut-ref="pc"></aop:before> <aop:after method="after" pointcut-ref="pc"></aop:after> <aop:after-returning method="afterReturning" pointcut-ref="pc"/> <aop:after-throwing method="afterThrowing" pointcut-ref="pc"/> </aop:aspect> </aop:config> </beans>
执行结果:
上一篇:
IDEA上Java项目控制台中文乱码