配置AOP步骤:
1. 注入通知bean
2. 使用<aop:config>表明开始AOP配置
3. 使用<aop:aspect>表明开始配置切面 属性:id-唯一标识,ref-指定通知类bean的id
4. 在<aop:aspect>内部使用对应的标签来指定通知的类型
<aop:before>:配置前置通知
method属性:使用通知类的哪个方法
pointcut属性:用于指定切入点表达式,指明对业务类的哪些方法进行增强
切入点表达式结构
pointcut="execution(访问修饰符 返回值类型 包名+类名+方法名(参数列表))"
例如
pointcut="execution(public void daoImpl.UserDaoImpl.save())"
通常写法:一直到业务层的所有方法
* service.*.*(..)
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="logger" class="utils.Logger"/>
<aop:config>
<aop:aspect id="loggerAdvice" ref="logger">
<aop:before method="log" pointcut="execution(* daoImpl.UserDaoImpl.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
留言