Aop(基于XML配置文件实现)
来源:互联网 发布:假如没有石油 知乎 编辑:程序博客网 时间:2024/05/22 20:34
接口类public interface UserService { void queryUsers(); void saveUser();}
实现类public class UserServiceImp implements UserService { @Override public void queryUsers() { System.out.println("用户查询"); } @Override public void saveUser() { System.out.println("保存用户"); }}
通知类public class LogAdvice{ public void before() { System.out.println("== before =="); } public void after() { System.out.println("== after =="); } public void afterReturning(){ System.out.println("== afterReturning =="); } public void afterThrows(){ System.out.println("== afterThrows =="); } // 环绕通知 public void around(ProceedingJoinPoint joinPoint){ try { System.out.println("== 环绕通知(前) =="); joinPoint.proceed(); // 执行原方法 System.out.println("== 环绕通知(后) =="); } catch (Throwable e) { throw new RuntimeException(e); } }}
applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <!-- 配置UserService对象--> <bean id="userService" class="cn.com.spring.l_aop_xml.UserServiceImp"></bean> <!-- 配置一个通知对象 --> <bean id="logAdvice" class="cn.com.spring.l_aop_xml.LogAdvice"></bean> <!-- AOP有关的配置都 在aop:config中 --> <aop:config> <!-- 声明一个切面 --> <aop:aspect ref="logAdvice"> <!-- 声明切入点 --> <aop:pointcut expression="execution(* save*(..))" id="myPointCut"/> <!-- 指定在某切入点执行某操作 --> <aop:after method="after" pointcut-ref="myPointCut"/> <aop:before method="before" pointcut-ref="myPointCut"/> <aop:after-returning method="afterReturning" pointcut-ref="myPointCut"/> <aop:after-throwing method="afterThrows" pointcut-ref="myPointCut"/> <aop:around method="around" pointcut-ref="myPointCut"/> </aop:aspect> </aop:config></beans>
测试类public class test { // 使用Xml方式实现AOP @Test public void test() throws Exception { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml", getClass()); UserService userService = (UserService) app.getBean("userService"); System.out.println(userService.getClass()); userService.saveUser(); System.out.println(); userService.queryUsers(); System.out.println(); }}
注:通知类方法全部写上后的执行顺序
== before ==
== 环绕通知(前) ==
保存用户
== after ==
== afterReturning ==
== 环绕通知(后) ==
用户查询
0 0
- Aop(基于XML配置文件实现)
- (一)Spring AOP:基于XML配置文件
- Spring AOP - 基于XML配置文件
- 基于XML实现AOP
- 基于xml方式配置aop的配置文件
- Spring中的AOP(六)——基于XML配置文件方式的AOP
- Spring中的AOP(七)——基于XML配置文件方式的AOP
- Spring中的AOP(六)——基于XML配置文件方式的AOP
- Spring-通过xml配置文件实现切面(AOP)
- Spring AOP(3)基于XML配置实现的示例
- Spring AOP(3)基于XML配置实现的示例
- 基于XML方式的AOP实现
- spring -- aop基于xml的简单实现
- Struts2基于XML配置文件实现输入校验
- Struts2基于XML配置文件实现权限校验
- spring.AOP(XML实现)
- Spring - AOP基于XML
- Spring-AOP:基于配置文件方式的AOP
- ORA-15041,ORA-01274 故障一例
- VMWare 提示“内部错误”的解决方案
- 嵌入式学习笔记300-linux根文件系统搭建
- 牛腩新闻发布系统-真假分页
- python的编译
- Aop(基于XML配置文件实现)
- #linux学习笔记#makefile与源文件不一目录解决办法
- 傻逼一样的行为——交友需谨慎
- shell编程复习 第一次
- 机房收费系统 重构(5)——数据库操作(存储过程)
- Lua中的协程coroutine简介
- 关于Java集合
- 集合
- 5-4