创建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>
测试输出: