AspectJ的XML方式完成AOP的开发之AOP的通知类型

时间:2023-01-08 05:52:45
1. 前置通知
* 在目标类的方法执行之前执行。
* 配置文件信息:<aop:after method="before" pointcut-ref="myPointcut3"/>
* 应用:可以对方法的参数来做校验 2. 最终通知
* 在目标类的方法执行之后执行,如果程序出现了异常,最终通知也会执行
* 在配置文件中编写具体的配置:<aop:after method="after" pointcut-ref="myPointcut3"/>
* 应用:例如像释放资源 3. 后置通知
* 方法正常执行后的通知。
* 在配置文件中编写具体的配置:<aop:after-returning method="afterReturning" pointcut-ref="myPointcut2"/>
* 应用:可以修改方法的返回值 4. 异常抛出通知
* 在抛出异常后通知
* 在配置文件中编写具体的配置:<aop:after-throwing method="afterThorwing" pointcut-ref="myPointcut3"/>
* 应用:包装异常的信息 5. 环绕通知
* 方法的执行前后执行。
* 在配置文件中编写具体的配置:<aop:around method="around" pointcut-ref="myPointcut2"/>
* 要注意:目标的方法默认不执行,需要使用ProceedingJoinPoint对来让目标对象的方法执行。 代码举例:
(1)创建接口CustomerDao:
package com.huida.demo3;

public interface CustomerDao {

    public void save();
public void update();
}
(2)创建实现接口的CustomerDaoImpl类:
package com.huida.demo3;

public class CustomerDaoImpl implements CustomerDao{

    @Override
public void save() {
//int i=1/0;
System.out.println("添加客户");
}
@Override
public void update() {
System.out.println("更新客户");
} }
(3)创建切面类
package com.huida.demo3;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspectXml {

    //前置通知:方法在目标对象方法之前执行,这里目标方法为save方法
public void log(){
System.out.println("添加日志");
}
//最终通知:不管出不出现异常,都会执行
public void after(){
System.out.println("最终通知执行");
}
/*
* 后置通知:方法执行之后,执行后置通知,程序出现异常,后置通知不会执行
*/
public void afterReturn(){
System.out.println("后置通知");
}
/*
* 异常通知:出现异常之后执行
*/ /*
* 环绕通知
*/
public void around(ProceedingJoinPoint point){
System.out.println("环绕通知1");
//手动的让目标对象的方法执行,这里的目标对象方法为save()
try {
point.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("环绕通知2");
}
}

(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 definitions here -->
<!-- 配置客户dao -->
<bean id="customerDao" class="com.huida.demo3.CustomerDaoImpl"></bean>
<!-- 配置切面类 -->
<bean id="myAspectXml" class="com.huida.demo3.MyAspectXml"></bean>
<!-- 配置AOP -->
<aop:config>
<!--切面类 -->
<aop:aspect ref="myAspectXml">
<!-- 是在原始方法的前面执行,还是后面执行 -->
<!-- pointcut:切入点 -->
<!-- 切入点表达式
1.execution()固定的 不能不写
2.public可以省略不写,但如果是private则必须要写
3.void 返回值可以出现*表示任意的返回值 。返回值类型不能不写
4.可以使用*代替, 必须编写。如果想找这个项目中所有的方法,不能只写一个*,而应写为*..*
5.类名 可以写为*DaoImpl,表示拦截以DaoImpl的方法
6.方法名 save* 拦截方法以save开头的方法。
7.方法的参数
-->
<!-- <aop:before method="log" pointcut="execution(public void com.huida.demo3.CustomerDaoImpl.save*(..))"/> -->
<!-- <aop:after method="after" pointcut="execution(public void com.huida.demo3.CustomerDaoImpl.save*(..))"/> -->
<!-- <aop:after-returning method="afterReturn" pointcut="execution(public void com.huida.demo3.CustomerDaoImpl.save*(..))"/> -->
<aop:around method="around" pointcut="execution(public void com.huida.demo3.CustomerDaoImpl.save*(..))"/> </aop:aspect>
</aop:config> </beans>

(5)测试代码为:

package com.huida.demo3;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext3.xml")
public class demo3 { @Resource(name="customerDao")
private CustomerDao customerDao;
@Test
public void run1(){
customerDao.save();
customerDao.update();
}
}

(6)执行结果:

   a.前置通知的执行结果:

    AspectJ的XML方式完成AOP的开发之AOP的通知类型

   b.最终通知的执行结果:

    AspectJ的XML方式完成AOP的开发之AOP的通知类型

   c.后置通知的执行结果:

    AspectJ的XML方式完成AOP的开发之AOP的通知类型

   d.环绕通知的执行结果:

    AspectJ的XML方式完成AOP的开发之AOP的通知类型