基于XML的AOP配置(常用)。
- IUserService接口
package com.qfedu.aop05;
import java.util.List;
/**
* Created by HuangCong on 2020/2/26.
*/
public interface IUserService {
List<Object> getAllUser();
boolean saveUser(Object user);
boolean deleteUser(int uid);
boolean updateUser(Object obj);
}
- UserServiceImpl类
package com.qfedu.aop05;
import java.util.List;
/**
* Created by HuangCong on 2020/2/26.
*/
public class UserServiceImpl implements IUserService {
@Override
public List<Object> getAllUser() {
System.out.println("------getAllUser------");
return null;
}
@Override
public boolean saveUser(Object user) {
System.out.println("------saveUser------");
return false;
}
@Override
public boolean deleteUser(int uid) {
System.out.println("------deleteUser------");
return false;
}
@Override
public boolean updateUser(Object obj) {
System.out.println("------updateUser------");
return false;
}
}
- MyAspect:切面类
package com.qfedu.aop05;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* 注意:如果同时配置来所有的通知方式,则执行顺序依次为:
* before>around before>业务方法 >(after throwing)是否引发异常 >after returning > around after > after
*
* ps. 使用注解的话是环绕通知proceed方法之前部分先执行,使用xml配置的话取决于aop:before和aop:around的配置顺序
* Created by HuangCong on 2020/2/27.
*/
public class MyAspect {
// 前置通知
// JoinPoint:连接点
public void myBefore(JoinPoint jp) {
System.out.println("============Before============");
}
// 后置通知
public void myAfter(JoinPoint jp) {
System.out.println("============After===========");
}
// 环绕通知
// ProceedingJoinPoint:作为参数:定义增强处理方法时,将第一个参数定义为该类型,当该增强处理方法被调用时,该类型的对象就代表织入增强处理的连接点。
public Object myAround(ProceedingJoinPoint pjp) {
try {
System.out.println("=========Around Before=========");
Object obj = pjp.proceed();
System.out.println("=========Around After==========");
return obj;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
// 最终通知:带有返回值的通知,obj是配置文件中的obj
public void myReturn(JoinPoint jp, Object obj) {
System.out.println("==============Return=============" + obj);
}
public void myThrow(JoinPoint jp, Throwable e) {
System.out.println("============Throw============" + e.getMessage());
}
}
- beans_aop05.xml: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">
<bean id="us" class="com.qfedu.aop05.UserServiceImpl"/>
<bean id="ma" class="com.qfedu.aop05.MyAspect"/>
<aop:config proxy-target-class="true">
<aop:aspect ref="ma">
<aop:pointcut id="mpc" expression="execution(* com.qfedu.aop05.*.*(..))"/>
<aop:before method="myBefore" pointcut-ref="mpc"/>
<aop:after method="myAfter" pointcut-ref="mpc"/>
<aop:around method="myAround" pointcut-ref="mpc"/>
<aop:after-returning method="myReturn" pointcut-ref="mpc" returning="obj"/>
<aop:after-throwing method="myThrow" pointcut-ref="mpc" throwing="e"/>
</aop:aspect>
</aop:config>
</beans>
- TestAOP05
package com.qfedu.aop05;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by HuangCong on 2020/2/27.
*/
public class TestAOP05 {
@Test
public void testAOP05() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans_aop05.xml");
IUserService us = context.getBean("us", IUserService.class);
Object obj = new Object();
us.getAllUser();
us.saveUser(obj);
us.deleteUser(1);
us.updateUser(obj);
}
}