實現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>
執行結果: