一、简单应用
1、建工程,导包
2、写目标对象
package top.einino.service;
public interface PersonService {
void eat();
}
package top.einino.service.impl;
import top.einino.service.PersonService;
public class PersonServiceImpl implements PersonService{
@Override
public void eat(){
System.out.println("吃饭!!");
//int i = 1/0;
}
}
3、写切面
package top.einino.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class PersonAspect {
//前置通知 参数为连接点对象,如果抛出异常,默认方法不能执行
public void before(JoinPoint joinPoint){
//连接点信息
System.out.println(joinPoint.toLongString());
System.out.println("前置通知:买菜");
}
//后置通知,第一个参数是连接点对象,第二个参数是方法返回值,参数名必须与配置文件的名字一样
public void afterReturning(JoinPoint joinPoint, Object result){
System.out.println("后置通知:目标返回值"+result);
}
//环绕通知,参数为可执行的连接点对象,可以实现任何通知效果
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕通知执行前:做饭");
Object result = proceedingJoinPoint.proceed();
System.out.println("环绕通知执行后:清洗");
}
//异常通知,第一个参数为连接点对象,第二个参数为异常对象
public void afterThrowing(JoinPoint joinPoint, Exception e){
//得到方法名
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法发生异常"+e);
}
//最终final通知,无论该方法是否出现异常,该方法总是会执行,被运用于释放资源
public void after(JoinPoint joinPoint){
System.out.println("结束整个流程");
}
}
4、写配置文件
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- bean实例化 -->
<!-- 配置目标对象 -->
<bean id="personService" class="top.einino.service.impl.PersonServiceImpl"/>
<!-- 配置切面bean -->
<bean id="personAspect" class="top.einino.aspect.PersonAspect"/>
<!-- aop配置 -->
<aop:config>
<aop:pointcut expression="execution(* top.einino.service.impl.PersonServiceImpl.*(..))" id="personPointcut"/>
<!-- ref定义切面 -->
<aop:aspect ref="personAspect">
<!-- 前置通知 -->
<aop:before method="before" pointcut-ref="personPointcut"/>
<!-- 环绕通知 -->
<aop:around method="around" pointcut-ref="personPointcut"/>
<!-- 后置通知 returning值要与方法参数名一致-->
<aop:after-returning method="afterReturning" returning="result" pointcut-ref="personPointcut"/>
<!-- 异常抛出通知 -->
<aop:after-throwing method="afterThrowing" throwing="e" pointcut-ref="personPointcut"/>
<!-- 最终final通知 -->
<aop:after method="after" pointcut-ref="personPointcut"/>
</aop:aspect>
</aop:config>
</beans>
5、测试
package top.einino.junit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import top.einino.service.PersonService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class TestPersonAspect {
@Autowired
PersonService personService;
@Test
public void testEat(){
personService.eat();
}
}
二、小结
本博文简单介绍了aspectj aop的简单使用过程,AspectJ 是一个框架 (第三方AOP框架 ),提供切面编程 ,编写一个Aspect 支持多个Advice和多个PointCut 。在配置文件中,方法定义顺序不同,方法切入过程也会不同,像本博文的定义顺序,在不抛异常的情况下会依次执行before,around(方法执行前后),afterReturning,after;在抛异常的情况下会依次执行before,around(方法执行前),afterThrowing,after。
如果有疑问或者对该博文有何看法或建议或有问题的,欢迎评论,恳请指正!