1、导入坐标依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.8.RC3</version>
2、编写接口
public interface Car {
public void brand(String brand);
public void color(String color);
public void throwex() throws Exception;
}
3、编写接口的实现类
public class CarImpl implements Car{
@Override
public void brand(String brand) {
System.out.println("这是"+brand+".........");
}
@Override
public void color(String color) {
System.out.println("颜色是"+color+"............");
}
@Override
public void throwex() throws Exception {
try {
System.out.println("这是throwex方法。。");
int x = 1/0;
}catch (Exception e){
throw new Exception(e.getMessage());
}
}
}
4、编写切面类
public class LogHandler {
public void handler(JoinPoint joinPoint){
System.out.println("这是通知。。。。。");
}
}
import org.aspectj.lang.JoinPoint;
public class LogThrowHandler {
public void displayException(JoinPoint joinPoint,Exception e){
System.out.println("这是异常通知。。。。。。");
}
}
import org.aspectj.lang.ProceedingJoinPoint;
public class LogAroundHandler {
public Object logAroundHandler(ProceedingJoinPoint joinPoint) throws Exception {
System.out.println("这是前置通知");
try {
Object result = joinPoint.proceed();
System.out.println("操作成功之后的通知");
return result;
}catch (Throwable throwable){
System.out.println("这是有异常的通知");
throw new Exception(throwable.getMessage());
}
}
}
5、配置bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="carImpl" class="com.woniuxy.aop2.CarImpl"/>
<!--配置切面类-->
<bean id="logHandler" class="com.woniuxy.aop2.LogHandler"/>
<bean id="logThrowHandler" class="com.woniuxy.aop2.LogThrowHandler"/>
<bean id="logAroundHandler" class="com.woniuxy.aop2.LogAroundHandler"/>
<!--配置aop-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="carPointCut" expression="execution(public void com.woniuxy.aop2.Car.brand(String))"/>
<aop:pointcut id="carPointCutColor" expression="execution(public void com.woniuxy.aop2.Car.color(String))"/>
<aop:pointcut id="afterReturning" expression="execution(public void com.woniuxy.aop2.Car.throwex())"/>
<aop:pointcut id="logArountPointCut" expression="execution(public void com.woniuxy.aop2.Car.throwex())"/>
<!--配置切面上的切入点-->
<aop:aspect ref="logHandler">
<aop:before method="handler" pointcut-ref="carPointCut"/>
<!-- <aop:after method="handler" pointcut-ref="carPointCut"/>-->
<aop:after method="handler" pointcut-ref="carPointCutColor"/>
</aop:aspect>
<!-- <aop:aspect ref="logThrowHandler">-->
<!-- <aop:after-throwing method="displayException" pointcut-ref="afterReturning" throwing="e"/>-->
<!-- </aop:aspect>-->
<aop:aspect ref="logAroundHandler">
<aop:around method="logAroundHandler" pointcut-ref="logArountPointCut"/>
</aop:aspect>
</aop:config>
</beans>
6、编写测试类
@Test
public void test01() throws Exception {
Car car = (Car) ac.getBean("carImpl");
car.brand("黑旗。。。");
car.color("yellow");
car.throwex();
}
7、测试结果