ProductDao.java
package com.shunli.aspect.demo2;
public class ProductDao {
public void save(){
System.out.println("save......");
}
public String update(){
System.out.println("update......");
return "update return";
}
public void find(){
System.out.println("find......");
}
public void findAll(){
System.out.println("findAll......");
}
public void delete(){
System.out.println("delete......");
int i = 1/0;//异常
}
}
MyAspectXml.java
package com.shunli.aspect.demo2;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
/**
* @author shunli
* 切面类
*/
@Aspect
public class MyAspectXml {
//前置通知
public void before(JoinPoint joinPoint){
System.out.println("前置通知......" + joinPoint);
}
//后置通知
public void afterReturing(Object result){
System.out.println("后置通知=================="+result);
}
//环绕通知
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前通知================");
// 执行目标方法
Object obj = joinPoint.proceed();
System.out.println("环绕后通知================");
return obj;
}
//最终通知
public void after(){
System.out.println("最终通知==================");
}
//异常抛出通知
public void afterThrowing (Throwable e){
System.out.println("异常抛出通知==============" + e.getMessage());
}
}
applictionContext2.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" 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">
<!--XML配置方式完成AOP-->
<!--目标类-->
<bean id="productDao" class="com.shunli.aspect.demo2.ProductDao"/>
<!--定义切面-->
<bean id="myAspectXml" class="com.shunli.aspect.demo2.MyAspectXml"/>
<!--aop的相关配置=================-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="pointcut1" expression="execution(* com.shunli.aspect.demo2.ProductDao.save(..))"/>
<aop:pointcut id="pointcut2" expression="execution(* com.shunli.aspect.demo2.ProductDao.update(..))"/>
<aop:pointcut id="pointcut3" expression="execution(* com.shunli.aspect.demo2.ProductDao.find(..))"/>
<aop:pointcut id="pointcut4" expression="execution(* com.shunli.aspect.demo2.ProductDao.findAll(..))"/>
<aop:pointcut id="pointcut5" expression="execution(* com.shunli.aspect.demo2.ProductDao.delete(..))"/>
<!--配置AOP的切面-->
<aop:aspect ref="myAspectXml">
<!--配置前置通知-->
<aop:before method="before" pointcut-ref="pointcut1"/>
<!--配置后置通知-->
<aop:after-returning method="afterReturing" pointcut-ref="pointcut2" returning="result"/>
<!--配置环绕通知-->
<aop:around method="around" pointcut-ref="pointcut3"/>
<!--配置最终通知-->
<aop:after method="after" pointcut-ref="pointcut4"/>
<!--配置异常抛出通知-->
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut5" throwing="e"/>
</aop:aspect>
</aop:config>
</beans>
SpringDemo2.java
package com.shunli.aspect.demo2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applictionContext2.xml")
public class SpringDemo2 {
@Resource(name = "productDao")
private ProductDao productDao;
@Test
public void demo1(){
productDao.save();
productDao.update();
productDao.find();
productDao.findAll();
productDao.delete();
}
}
输出结果
Spring 基于Aspectj的注解开发AOP实例参考帖子
https://blog.csdn.net/shunli008/article/details/100056647