基于xml的aop配置(前置通知的使用方法)
建个maven项目 写个接口 和实现类 再写一个记录日志的类
建一个xml文件 约束去spring.IO搜xmlns:aop 拷贝
然后将实现类配置到xml里面 交给spring容器创建对象
把日志记录的类也配置进去
下面演示的还是一个前置通知使用 就是在保存用户之前调用日志方法
<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">
<!-- this is the object that will be proxied by Spring's AOP infrastructure -->
<bean id="acServieImpl" class="com.itheima.service.impl.AcServieImpl"/>
<!-- this is the actual advice itself
spring 基于xml的aop配置步骤
1.把通知bean交给spring来管理
2.使用aop:config标签标明开始配置aop配置
3.是使用aop:aspect标明配置切面
id属性: 给切面指定一个唯一标识
ref属性:指定通知类bean的id
-->
<bean id="Logger" class="com.itheima.log.Logger"/>
<aop:config>
<aop:aspect ref="Logger">
<!--配置切入点表达式-->
<aop:before method="printLog" pointcut="execution(public void com.itheima.service.impl.AcServieImpl.saveAccount())"></aop:before>
</aop:aspect>
</aop:config>
</beans>
测试结果
可以看到记录日志在保存用户操作之前执行力