AspectJ实现了AOP功能,使用方便,支持注解。Spring中也有AspectJ的方式实现AOP。在Spring中使用AOP开发时,一般使用AspectJ的实现方式
AspectJ的切点表达式:
execution(A, B, C, D, E);
[权限修饰符 可选]
A:方法返回类型
B:包名
C:类名
D:方法名
E:参数名
[异常类型 可选]
包名,类名,方法名要用.隔开,靠近()的方法名
execution(* com.target.ServiceImpl.*(…))
*:0至多个
… :用于参数,表示多个;用于包名后,表示这个包以及它的子包们
+:用于类,表示这个类以及它的子类们;用于接口,表示这个接口以及接口的实现类们
Eg:
execution(public * (. .))
指定切入点为:任意公共方法。
execution( set (. .))
指定切入点为:任何一个以“set”开始的方法。
execution( com.xyz.service..(. .))
指定切入点为:定义在service包里的任意类的任意方法。
execution(* com.xyz.service. ..(. .))
指定切入点为:定义在service包或者子包里的任意类的任意方法。“…”出现在类名中时,
后面必须跟“”,表示包、子包下的所有类。
execution( .service..(. .))
指定只有一级包下的serivce子包下所有类(接口)中的所有方法为切入点
execution( . .service..*(. .))
指定所有包下的serivce子包下所有类(接口)中的所有方法为切入点
1、注解的形式
通知类:必须加上@Aspect
这里的 returning="msg"必须和方法里面的参数同名,因为它就是目标类(切点)所返回(return)的值,throwing="ex"是目标类(切点所抛的异常)
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 MethodAdvice {
@Before("execution(* com.service.*.*(..))")
public void methodBefor() {
// TODO Auto-generated method stub
System.out.println("前置通知");
}
@AfterReturning(value="execution(* com.service.*.*(..))",returning="msg")
public void methodAfterReturn(String msg) {
// TODO Auto-generated method stub
System.out.println("后置通知:"+msg);
}
@Around(value="execution(* com.service.*.*(..))")
public Object methodAround(ProceedingJoinPoint pjp) throws Throwable {
// TODO Auto-generated method stub
System.out.println("方法前...");
Object proceed = pjp.proceed();
System.out.println("方法后..."+proceed);
return proceed;
}
@AfterThrowing(value="execution(* com.service.*.*(..))",throwing="ex")
public void methodThrow(Exception ex) {
// TODO Auto-generated method stub
System.out.println("出现异常:"+ex.getMessage());
}
@After("execution(* com.service.*.*(..))")
public void after() {
// TODO Auto-generated method stub
System.out.println("最终通知...");
}
}
目标类:(切点)
@Service
public class ServiceImpl implements IService{
@Override
public String say() {
// TODO Auto-generated method stub
return "SAY";
}
@Override
public String sleep() {
// TODO Auto-generated method stub
return "SLEEP";
}
}
applicationContext.xml:
<?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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.advice,com.service"/>
<!-- 开启AOP注解 -->
<aop:aspectj-autoproxy/>
</beans>
测试:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.IService;
public class Test {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
IService bean = (IService) applicationContext.getBean("serviceImpl");
System.out.println(bean.say());
System.out.println(bean.sleep());
}
}
2、xml配置的形式:
通知类:
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 MethodAdvice {
// @Before("execution(* com.service.*.*(..))")
public void methodBefor() {
// TODO Auto-generated method stub
System.out.println("前置通知");
}
/**
*这里的 returning="msg"必须和方法里面的参数同名,因为它就是目标类(切点)所返回(return)的值
* @param msg
*/
// @AfterReturning(value="execution(* com.service.*.*(..))",returning="msg")
public void methodAfterReturn(String msg) {
// TODO Auto-generated method stub
System.out.println("后置通知:"+msg);
}
// @Around(value="execution(* com.service.*.*(..))")
public Object methodAround(ProceedingJoinPoint pjp) throws Throwable {
// TODO Auto-generated method stub
System.out.println("方法前...");
Object proceed = pjp.proceed();
System.out.println("方法后..."+proceed);
return proceed;
}
// @AfterThrowing(value="execution(* com.service.*.*(..))",throwing="ex")
public void methodThrow(Exception ex) {
// TODO Auto-generated method stub
System.out.println("出现异常:"+ex.getMessage());
}
// @After("execution(* com.service.*.*(..))")
public void after() {
// TODO Auto-generated method stub
System.out.println("最终通知...");
}
}
目标类(切点)
@Service
public class ServiceImpl implements IService{
@Override
public String say() {
// TODO Auto-generated method stub
return "SAY";
}
@Override
public String sleep() {
// TODO Auto-generated method stub
return "SLEEP";
}
}
ApplicationContext.xml
<?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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.service,com.advice"/>
<aop:config>
<!-- 切面 -->
<aop:aspect ref="methodAdvice">
<!-- 切点 -->
<aop:pointcut expression="execution(* com.service.ServiceImpl.*(..))" id="myPointcut"/>
<!-- 前置通知 -->
<aop:before method="methodBefor" pointcut-ref="myPointcut"/>
<!-- 后置通知 -->
<aop:after-returning method="methodAfterReturn" pointcut-ref="myPointcut" returning="msg"/>
<!-- 异常通知 -->
<aop:after-throwing method="methodThrow" pointcut-ref="myPointcut" throwing="ex"/>
<!-- 环绕通知 -->
<aop:around method="methodAround" pointcut-ref="myPointcut"/>
<!-- 最终通知 -->
<aop:after method="after" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>
测试:
public class Test {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
IService bean = (IService) applicationContext.getBean("serviceImpl");
System.out.println(bean.say());
System.out.println(bean.sleep());
}
}