建议先看一下:
1.代理模式 https://blog.csdn.net/dengjili/article/details/82802614
2.spring aop入门 和 代理模式的衔接 https://blog.csdn.net/dengjili/article/details/82804163
在Aspectj 需要引入包
aspectjweaver
aspectjrt
下载相关代码,可运行
相关代码
https://github.com/dengjili/springaop
注解方式
实际拦截类
package priv.dengjl.aspectj.impl;
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;
@Aspect
public class ServiceAspectj {
@Before("execution(* priv.dengjl.springaop.service.*.printName(..))")
public void logBefore(JoinPoint point) {
System.out.println("Before() 正在运行!");
System.out.println("当前方法 : " + point.getSignature().getName());
}
@After("execution(* priv.dengjl.springaop.service.*.printURL(..))")
public void beforeAdvice() {
System.out.println("after() 正在运行!");
}
@AfterThrowing(pointcut = "execution(* priv.dengjl.springaop.service.*.printThrowException(..))", throwing= "error")
public void afterThrowing(Throwable error) {
System.out.println("异常返回信息:" + error.getMessage());
}
@AfterReturning(pointcut = "execution(* priv.dengjl.springaop.service.*.returnValue(..))", returning="result")
public void afterReturning(JoinPoint joinPoint,Object result) {
System.out.println("返回值信息:" + result);
}
@Around("execution(* priv.dengjl.springaop.service.*.testAroud(..))")
public void testAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("前");
joinPoint.proceed();
System.out.println("后");
}
}
对应配置文件
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<!-- 基础bean -->
<bean id="testService" class="priv.dengjl.springaop.service.TestService">
<property name="name" value="呵呵哒" />
<property name="url" value="www.mmp.com" />
</bean>
<!-- 开启aspectj方式 -->
<aop:aspectj-autoproxy />
<!-- Aspect -->
<bean id="testAspect" class="priv.dengjl.aspectj.impl.ServiceAspectj" />
</beans>
测试类
package priv.dengjl.aspectj;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import priv.dengjl.springaop.service.TestService;
public class App {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] { "applicationContextAspectj.xml" });
TestService cust = (TestService) appContext.getBean("testService");
System.out.println("*************************");
cust.printName();
System.out.println("*************************");
cust.printURL();
System.out.println("*************************");
try {
cust.printThrowException();
} catch (Exception e) {
System.out.println("有异常发生");
System.out.println("*************************");
}
cust.returnValue();
cust.testAroud();
}
}
测试结果
*************************
Before() 正在运行!
当前方法 : printName
名称 : 呵呵哒
*************************
网址 : www.mmp.com
after() 正在运行!
*************************
异常返回信息:1234
有异常发生
*************************
返回值信息:abc
前
11111111111144444444444
后
xml配置方法
实际拦截类
package priv.dengjl.aspectj.impl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class ServiceAspectjXml {
public void logBefore(JoinPoint point) {
System.out.println("Before() 正在运行!");
System.out.println("当前方法 : " + point.getSignature().getName());
}
public void afterAdvice() {
System.out.println("after() 正在运行!");
}
public void afterThrowing(Throwable error) {
System.out.println("异常返回信息:" + error.getMessage());
}
public void afterReturning(JoinPoint joinPoint,Object result) {
System.out.println("返回值信息:" + result);
}
public void testAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("前");
joinPoint.proceed();
System.out.println("后");
}
}
配置文件
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<!-- 基础bean -->
<bean id="testService" class="priv.dengjl.springaop.service.TestService">
<property name="name" value="呵呵哒" />
<property name="url" value="www.mmp.com" />
</bean>
<!-- 开启aspectj方式 -->
<aop:aspectj-autoproxy />
<!-- Aspect -->
<bean id="testAspect" class="priv.dengjl.aspectj.impl.ServiceAspectjXml" />
<aop:config>
<aop:aspect id="myAspect" ref="testAspect">
<aop:pointcut id="pointCutBefore"
expression="execution(* priv.dengjl.springaop.service.*.printName(..))" />
<aop:pointcut id="pointCutAfter"
expression="execution(* priv.dengjl.springaop.service.*.printURL(..))" />
<aop:pointcut id="pointCutAfterReturning"
expression="execution(* priv.dengjl.springaop.service.*.returnValue(..))" />
<aop:pointcut id="pointCutAfterThrowing"
expression="execution(* priv.dengjl.springaop.service.*.printThrowException(..))" />
<aop:pointcut id="pointCutAround"
expression="execution(* priv.dengjl.springaop.service.*.testAroud(..))" />
<!-- @Before -->
<aop:before method="logBefore" pointcut-ref="pointCutBefore" />
<!-- @After -->
<aop:after method="afterAdvice" pointcut-ref="pointCutAfter" />
<!-- @AfterReturning -->
<aop:after-returning method="afterReturning"
returning="result" pointcut-ref="pointCutAfterReturning" />
<!-- @AfterThrowing -->
<aop:after-throwing method="afterThrowing"
throwing="error" pointcut-ref="pointCutAfterThrowing" />
<!-- @Around -->
<aop:around method="testAround" pointcut-ref="pointCutAround" />
</aop:aspect>
</aop:config>
</beans>
测试类
package priv.dengjl.aspectj;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import priv.dengjl.springaop.service.TestService;
public class AppXml {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] { "applicationContextAspectjXml.xml" });
TestService cust = (TestService) appContext.getBean("testService");
System.out.println("*************************");
cust.printName();
System.out.println("*************************");
cust.printURL();
System.out.println("*************************");
try {
cust.printThrowException();
} catch (Exception e) {
System.out.println("有异常发生");
System.out.println("*************************");
}
cust.returnValue();
cust.testAroud();
}
}
测试结果
*************************
Before() 正在运行!
当前方法 : printName
名称 : 呵呵哒
*************************
网址 : www.mmp.com
after() 正在运行!
*************************
异常返回信息:1234
有异常发生
*************************
返回值信息:abc
前
11111111111144444444444
后
AspectJ配置表达式
AspectJ最强大的地方在于他的切入点表达式:
语法:execution(修饰符 返回值 包.类.方法名(参数) throws异常)
描述 | 取值 |
---|---|
修饰符 | 一般省略,execution(修饰符 返回值 包.类.方法名(参数) throws异常) |
修饰符 | public 公共方法 |
修饰符 | * 任意 |
返回值 | 不能省略,execution(修饰符 返回值 包.类.方法名(参数) throws异常) |
返回值 | void 返回没有值 |
返回值 | String 返回值字符串 |
返回值 | * 任意 |
包 | 不能省略,execution(修饰符 返回值 包.类.方法名(参数) throws异常) |
包 | priv.dengjl.service 固定包 |
包 | priv.dengjl.oa.*.service oa包下面子包 (例如:priv.dengjl.oa.flow.service) |
包 | priv.dengjl.oa… oa包下面的所有子包(含自己) |
包 | priv.dengjl.oa.*.service… oa包下面任意子包,固定目录service,service目录任意包 |
类 | 不能省略,execution(修饰符 返回值 包.类.方法名(参数) throws异常) |
类 | UserServiceImpl 指定类 |
类 | *Impl 以Impl结尾 |
类 | User* 以User开头 |
类 | * 任意 |
方法名 | 不能省略,execution(修饰符 返回值 包.类.方法名(参数) throws异常) |
方法名 | addUser 固定方法 |
方法名 | add* 以add开头 |
方法名 | *Do 以Do结尾 |
方法名 | * 任意 |
参数 | 不能省略,execution(修饰符 返回值 包.类.方法名(参数) throws异常) |
参数 | () 无参 |
参数 | (int) 一个整型 |
参数 | (int ,int) 两个 |
参数 | (…) 参数任意 |
throws | 可省略,一般不写。,execution(修饰符 返回值 包.类.方法名(参数) throws异常)
详细的表达式用法,查看专业文档。