十二 Spring的AOP开发入门,整合Junit单元测试(AspectJ的XML方式)

创建web项目,引入jar包

 

引入Spring配置文件

 

编写目标类,完成配置

 

编写测试类

 

Spring整合Junit单元测试

 

编写一个切面类

配置切面类,产生代理:

<?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="productDao" class="com.spring4.demo3.ProductDaoImpl"></bean>

    <!-- 将切面类交给Spring管理 -->
    <bean id="myAspect" class="com.spring4.demo3.MyAspectXML">

    </bean>

    <!-- 通过Spring配置对目标类完成代理 -->
    <aop:config>
        <!-- 配置切入点,表达式配置哪些类的哪些方法需要进行增强,*代表任意返回值,...代表任意参数 -->
        <aop:pointcut id="pointcut1"
            expression="execution(* com.spring4.demo3.ProductDaoImpl.save(..))" />
        <!-- 配置切面 -->
        <aop:aspect ref="myAspect">
        <!-- 配置为前置增强 -->
            <aop:before method="checkPri" pointcut-ref="pointcut1" />
        </aop:aspect>
    </aop:config>
</beans>

 

测试输出:

 

转载于:https://www.cnblogs.com/ltfxy/p/9882430.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值