pring AOP 的简单例子
2015-08-26 11:34
477 查看
http://www.cnblogs.com/yql1986/articles/2213486.html
Spring AOP 实现了AOP联盟(Alliance)的制定的接口规范,它基于java的代理机制实现。AOP作为Spring的核心技术之一. 更多关于Spring AOP介绍 可参考:http://oss.org.cn/ossdocs/framework/spring/zh-cn/aop.html
下面给出一个例子来简单介绍Spring AOP具体实现过程
现假设用户通过login.jsp页面输入相应的用户名和密码之后,首先Spring AOP的环绕通知验证该用户名和密码是否符合要求,若符合要求,则到数据库中查找该用户,若用户存在,将该用户相关的信息写入日志。
1.BaseLoginAdvice 类实现了 前置通知接口(MethodBeforeAdvice)、环绕通知接口(MethodInterceptor)、后置通知接口(AfterReturningAdvice) 这三个接口
BaseLoginAdvice.java
2.LoginAdviceSupport 类继承 BaseLoginAdvice 类,并重写 BaseLoginAdvice 类的三个方法
LoginAdviceSupport.java
3. IUser.java
4.UserImpl.java
5. Spring 的配置文件 login.bean.xml
6.主程序文件 ConsoleApp.java
运行程序之后,控制台上输出的结果
通过结果可以看到每个函数的执行顺序。关于 前置通知、环绕通知、后置通知 这些术语 网上都有相关的介绍
Spring AOP 实现了AOP联盟(Alliance)的制定的接口规范,它基于java的代理机制实现。AOP作为Spring的核心技术之一. 更多关于Spring AOP介绍 可参考:http://oss.org.cn/ossdocs/framework/spring/zh-cn/aop.html
下面给出一个例子来简单介绍Spring AOP具体实现过程
现假设用户通过login.jsp页面输入相应的用户名和密码之后,首先Spring AOP的环绕通知验证该用户名和密码是否符合要求,若符合要求,则到数据库中查找该用户,若用户存在,将该用户相关的信息写入日志。
1.BaseLoginAdvice 类实现了 前置通知接口(MethodBeforeAdvice)、环绕通知接口(MethodInterceptor)、后置通知接口(AfterReturningAdvice) 这三个接口
BaseLoginAdvice.java
LoginAdviceSupport.java
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <bean id="loginAdvice" class="com.laoyangx.Aop.chapter0.LoginAdviceSupport"></bean> <bean id="userTarget" class="com.laoyangx.Aop.chapter0.UserImpl"></bean> <bean id="user" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.laoyangx.Aop.chapter0.IUser</value> </property> <property name="interceptorNames"> <list> <value>loginAdvice</value> </list> </property> <property name="target"> <ref bean="userTarget"/> </property> </bean> </beans>
6.主程序文件 ConsoleApp.java
通过结果可以看到每个函数的执行顺序。关于 前置通知、环绕通知、后置通知 这些术语 网上都有相关的介绍
相关文章推荐
- linux下android开发真机设备识别不了怎么办
- 介绍几个不错的linux下载站和论坛
- nginx
- eclipse 插件及其配置(jdk+eclipse+svn+maven+jetty+nginx)
- 解决 nginx: [alert] kill(189, 1) failed (3: No such process)
- QTC++监控USB插拔
- Linux shell 字符串常用操作
- willMoveToParentViewController和didMoveToParentViewController
- linux 学习文章
- Office 365 IT Professional——利用PowerShell设置Reset Password
- Debian Linux系统如何进入单用户模式恢复密码
- Hadoop之hdfs单机最简配置(Cent OS 7)
- 解析nginx负载均衡
- Hadoop-0.22.0分布式集群配置
- apache + subversion for linux 安装
- centos服务器操作要点
- linux挂载新硬盘,开机自动挂载
- BS和CS架构
- Linux单独安装svn服务器-subversion
- ext5学习网站