使用自定义的切面
- 切面的定义:切入的所有类的集合
public class DivPointCut {
public void before(){
System.out.println("方法执行前");
}
public void after(){
System.out.println("方法执行后");
}
}
- xml中配置切面
<aop:config>
<!--自定义切面,ref要引用的类-->
<aop:aspect ref="div">
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.yf.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
通知:定义了切入到的地方,在前还是后,以及切入的是哪个方法
- 最后进行测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口,要用接口类型来引用实现类对象
UserService userService = (UserService)context.getBean("userService");
userService.delete();
}
}
本文介绍了如何在Spring框架中通过XML配置自定义切面,包括定义切入点、前置通知和后置通知的方法,并通过具体示例展示了如何为指定的服务方法添加切面逻辑。

被折叠的 条评论
为什么被折叠?



