Spring-xml配置实现Aop

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、测试结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值