首页(162) xml(5) spring(1) 生活(8) java(70) 代码(10) 英语(4) 数据库(7) c#(14) 成长(10) 软件工程(27)  写新日志
 
 

玻璃杯中的花生壳

  真爱的事业和真正的爱情一生只有一次,都值得我们温柔地相待,因为那种感觉是永远都无法复制的, 这世界真正属于你的东西其实并不多,你不好好珍惜,它便会离你而去,包括机遇,包括爱情,包括生命。
   不要找任何理由,  当幸福在你身边的时候就抓住它,你就一定会很幸福! 
   

时 间 记 忆
«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31

最 新 评 论
回复:xml的Jdom解析过程详解
回复:突然想到的几句话!
 Boyle came out of n
回复:xml的Jdom解析过程详解
回复:配置Spring数据源
回复:使用SAX解析XML
回复:java中写文件操作时FileOu
回复:关联和依赖关系的区分
回复:HttpSessionListen
回复:Spring AOP四种创建通知(

最 新 日 志
Java开发者的十大戒律
配置Spring数据源
java多线程设计模式
java中switch的使用
性格,编码,测试
突然想到的几句话!
理解Spring AOP中的关键概念
Spring AOP四种创建通知(拦截器
xml的四种解析方法 比较 sax,do
xml的Jdom解析过程详解

最 新 留 言
签写新留言

我渴望知识
很好的东东
帖子不错,道声谢
想拜师学艺
我的呼喊

搜 索


用 户 登 录
用户名称:
登陆密码:
密码保存:

友 情 连 接

模板设计:部落窝模板世界

blog名称:玻璃杯中的花生壳
日志总数:162
评论数量:249
留言数量:1
访问次数:824522
建立时间:2004年11月4日
 
 
 
[java]理解Spring AOP中的关键概念
[ 2007/7/4 13:42:41 | By: 玻璃杯中的花生壳 ]
 
  这一周我都在用空余的时间来看Spring这个东西,突然觉得这个东西并没有想像中那么深不可测,最多只有AOP这方面的东西不是我所熟悉的,不过经过自己努力,也不是不可理解的。AOP,面向切面的编程,是一种从各个方面对软件系统进行关注的开发方法,也就是说,可以把要实现的系统从多个方面进行理解和开发。比如一个系统,它包含了业务逻辑,日志管理,安全管理(认证管理)等,一般而言,我在开发包含这些方面的程序时,就很容易进入单面一维的操作去:比如下面是一个用户管理程序的部份:1、验证用户是否已经登录2、记录日志3、进行操作,对重要的操作都可能要进行日志记录。4、记录用户离开时间......由上面的步骤可以看到,真正的业务逻辑只有第3步,其它的都是附加的功能,或者是从另一方面加入的功能,他们和业务逻辑是相辅相成,可是业务逻辑并不需要知道这些附加的功能,又从另一方面来看,每一进行的操作都会触发附加的操作,而且这些应该是自动的,业务逻辑所不知道的。所以就引进一个拦截器的概念,对用户的操作进行拦截,然后进行附加的操作。而用户的操作并不知道这些拦截器的存在,从而可以使开发业务逻辑的程序员可以专心地写他的业务逻辑,而其它程序员则专心写他的拦截器,从而就可以使程序员从多个方面进行关注,进行编程。而Spring AOP就起到了这个拦截器的作用,而这些就演化出很多的术语,也无非是拦截器而已,现在俗一点来理解这些个术语:切面:宏观上要关注的编程方面,比如安全管理方面,日志记录方面,而这些都只是宏观上的,没有任何代码实现,就好像这样说:我这个系统要一个安全管理系统等等,就是大至上需要这个切面功能。通知(Advice):切面功能的代码实现,也就是说具体的切面功能。连接点(Joinpoint):程序运行中可以把通知道插入的所有地方,比如某方法前,后,或抛出异常,而所以这些方法的插入点都是连接点。切入点(Pointcut):定义了通知可以插入到哪些连接点。Advisor:这里只有Spring才有,它包含了通知和切入点,以定义在什么连接点插入什么通知。下面我用一个代码例子来说明:文件:SayHelloService.java 1 package  com.bullonline.study.spring.ch03;2  3 public interface  SayHelloService {4     public void  printMessage(String message);5     public void  printInfomation(String info);6  }7  文件:SayHelloServiceImpl.java  1 package  com.bullonline.study.spring.ch03; 2   3 public class SayHelloServiceImpl implements  SayHelloService { 4   5     public void  printMessage(String message) { 6         System.out.println("message:" +  message); 7         System.out.println("---------------------------------\n" ); 8      } 9  10     public void  printInfomation(String info) {11         System.out.println("500)this.width=500'>500)this.width=500'>..Infomation500)this.width=500'>500)this.width=500'>500)this.width=500'>" );12         System.out.println("=      love is good       =" );13          System.out.println(info);14         System.out.println("---------------------------------\n" );15      }16  17  }18  文件:SayHelloAdvice.java  1 package  com.bullonline.study.spring.ch03; 2   3 import  java.lang.reflect.Method; 4   5 import  org.springframework.aop.MethodBeforeAdvice; 6   7 public class SayHelloAdvice implements  MethodBeforeAdvice { 8   9     public void  before(Method method, Object[] args, Object target)10             throws  Throwable {11         System.out.println(method.toString() + ": say hello to you!!" );12      }13  14 } 文件:MyClassFilter.java  1 package  com.bullonline.study.spring.ch03; 2   3 import  org.springframework.aop.ClassFilter; 4   5 public class MyClassFilter implements  ClassFilter { 6   7     public boolean  matches(Class target) { 8         return true ; 9      }10  11  }12  文件:MyMethodMatcher.java  1 package  com.bullonline.study.spring.ch03; 2   3 import  java.lang.reflect.Method; 4   5 import  org.springframework.aop.MethodMatcher; 6   7 public class MyMethodMatcher implements  MethodMatcher { 8   9     public boolean  matches(Method method, Class target) {10         String methodName =  method.getName();11         if (methodName.equals("printMessage" )) {12             return true ;13         } else  {14             return false ;15          }16      }17  18     public boolean  isRuntime() {19         return false ;20      }21  22     public boolean  matches(Method arg0, Class arg1, Object[] arg2) {23         return false ;24      }25  26  }27  文件:MyPointcut.java  1 package com.bullonline.study.spring.ch03; 2  3 import org.springframework.aop.ClassFilter; 4 import org.springframework.aop.MethodMatcher; 5 import org.springframework.aop.Pointcut; 6  7 public class MyPointcut implements Pointcut { 8     private ClassFilter filter; 9     private MethodMatcher matcher;10     11     public void setClassFilter(ClassFilter filter) {12         this.filter = filter;13     }14 15     public void setMethodMatcher(MethodMatcher matcher) {16         this.matcher = matcher;17     }18     19     public ClassFilter getClassFilter() {20         return this.filter;21     }22 23     public MethodMatcher getMethodMatcher() {24         return this.matcher;25     }26 27 }28 文件:MyPointcutAdvisor.java  1 package com.bullonline.study.spring.ch03; 2  3 import org.aopalliance.aop.Advice; 4 import org.springframework.aop.Pointcut; 5 import org.springframework.aop.PointcutAdvisor; 6  7 public class MyPointcutAdvisor implements PointcutAdvisor { 8     private Pointcut pointcut; 9     private Advice advice;10     11     public void setPointcut(Pointcut pointcut) {12         this.pointcut = pointcut;13     }14     15     public void setAdvice(Advice advice) {16         this.advice = advice;17     }18 19     public Pointcut getPointcut() {20         return this.pointcut;21     }22 23     public boolean isPerInstance() {24         return false;25     }26 27     public Advice getAdvice() {28         return this.advice;29     }30 31 }32 文件:TestApp.java  1 package com.bullonline.study.spring.ch03; 2  3 import org.springframework.beans.factory.BeanFactory; 4 import org.springframework.beans.factory.xml.XmlBeanFactory; 5 import org.springframework.core.io.ClassPathResource; 6  7 public class TestApp { 8     public static void main(String[] args) { 9         BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));10         SayHelloService service = (SayHelloService) factory.getBean("sayHelloService");11         service.printMessage("Hello, World!!");12         service.printInfomation("haha500)this.width=500'>500)this.width=500'>..");13         14     }15 16 }17 文件:applicationContext.xml  1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 3  4 <beans> 5     <bean id="helloWorld" class="com.bullonline.study.spring.ch01.HelloWorld"> 6         <property name="message" value="Haha500)this.width=500'>500)this.width=500'>.Hello, World!!"></property> 7     </bean> 8      9     <bean id="sayHelloServiceImpl" class="com.bullonline.study.spring.ch03.SayHelloServiceImpl" />10     <bean id="classFilter" class="com.bullonline.study.spring.ch03.MyClassFilter" />11     <bean id="methodMatcher" class="com.bullonline.study.spring.ch03.MyMethodMatcher" />12     <bean id="sayHelloAdvice" class="com.bullonline.study.spring.ch03.SayHelloAdvice" />13     14     <bean id="myPointcut" class="com.bullonline.study.spring.ch03.MyPointcut">15         <property name="classFilter">16             <ref bean="classFilter"/>17         </property>18         <property name="methodMatcher">19             <ref bean="methodMatcher"/>20         </property>21     </bean>22     23     <bean id="myPointcutAdvisor" class="com.bullonline.study.spring.ch03.MyPointcutAdvisor">24         <property name="pointcut">25             <ref bean="myPointcut"/>26         </property>27         <property name="advice">28             <ref bean="sayHelloAdvice"/>29         </property>30     </bean>31     32     <bean id="sayHelloService" class="org.springframework.aop.framework.ProxyFactoryBean">33         <property name="proxyInterfaces">
 

阅读全文(4931) | 回复(0) | 编辑 | 精华
 

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
 
部落窝Blog模板世界部落窝Blog模板世界
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.062 second(s), page refreshed 144763296 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号