一,导入相关jar包
二.xml方法的配置
(1).编写切入目标类
(2).编写通知类
import org.aspectj.lang.ProceedingJoinPoint;
/**
*
* @author Aurora
* 前置通知
*/
public class AspectXml {
public void checkasp() {
System.out.println("校验+++++======");
}
/*
* 后置通知
*/
public void log1(Object result) {
System.out.println("kkk"+result);
}
/*
*环绕通知
*/
public Object aroung(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("修改商品前");
//相当于执行目标方法
Object object= joinPoint.proceed();
System.out.println("修改商品后");
return object;
}
}
(2).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"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
<bean id="customer" class="com.cws.spring.demo2.Customer"></bean>
<bean id="aspect1" class="com.cws.spring.demo2.AspectXml"></bean>
<aop:config>
<!-- 通过execution指向切入点 -->
<aop:pointcut expression="execution(* com.cws.spring.demo2.Customer.save(..))" id="p2"/>
<aop:pointcut expression="execution(* com.cws.spring.demo2.Customer.delete(..))" id="p3"/>
<aop:pointcut expression="execution(* com.cws.spring.demo2.Customer.update(..))" id="p4"/>
<!-- 设置通知指向的类-->
<aop:aspect ref="aspect1">
<aop:before method="checkasp" pointcut-ref="p2" />
<aop:after-returning method="log1" pointcut-ref="p3" returning="result"/>
<aop:around method="aroung" pointcut-ref="p4"/>
</aop:aspect>
</aop:config>
</beans>