本帖最后由 java2000_net 于 2008-08-05 14:26:14 编辑

解决方案 »

  1.   

    一个简单的Spring的AOP例子 
    ???? ?经过这段日子的学习和使用Spring,慢慢地体会到Spring的优妙之处,正在深入地吸收Spring的精华,呵呵。现在写的这个只是个简单AOP例子,包括前置通知,后置通知,环绕通知,和目标对象。写这个例子的主要目标只是想让想学AOP的能更快地入门,了解一下如何去配置AOP里面的东东。
    目标对象的接口:IStudent.java?1   /**?*/ /** 
    ?2 ?*?
    ?3 ? */ 
    ?4  package ?com.dragon.study;
    ?5  
    ?6   /**?*/ /** 
    ?7 ?*? @author ?dragon
    ?8 ?*
    ?9 ? */ 
    10   public ? interface ?IStudent?  {
    11 ????
    12 ???? public ? void ?addStudent(String?name);
    13 } 
    14  目标类:StudentImpl.java?1   /**?*/ /** 
    ?2 ?*?
    ?3 ? */ 
    ?4  package ?com.dragon.study.Impl;
    ?5  
    ?6  import ?com.dragon.study.IStudent;
    ?7  
    ?8   /**?*/ /** 
    ?9 ?*? @author ?dragon
    10 ?*
    11 ? */ 
    12   public ? class ?StudentImpl? implements ?IStudent  {
    13  
    14  ????? public ? void ?addStudent(String?name)  {
    15 ?????????System.out.println( " 欢迎? " + name + " ?你加入Spring家庭! " );
    16 ?????} 
    17 } 
    18  
    前置通知:BeforeAdvice.java?1   /**?*/ /** 
    ?2 ?*?
    ?3 ? */ 
    ?4  package ?com.dragon.Advice;
    ?5  
    ?6  import ?java.lang.reflect.Method;
    ?7  
    ?8  import ?org.springframework.aop.MethodBeforeAdvice;
    ?9  
    10   /**?*/ /** 
    11 ?*? @author ?dragon
    12 ?*
    13 ? */ 
    14   public ? class ?BeforeAdvice? implements ?MethodBeforeAdvice  {
    15  
    16 ?????? public ? void ?before(Method?method,Object[]?args,?Object?target)
    17  ??????????????? throws ?Throwable  {
    18 ??????????
    19 ??????????System.out.println( " 这是BeforeAdvice类的before方法. " );
    20 ??????????
    21 ??????} 
    22 } 
    23  
    后置通知:AfterAdvice.java
    ?1/**?*//**
    ?2?*?
    ?3?*/
    ?4package?com.dragon.Advice;
    ?5
    ?6import?java.lang.reflect.Method;
    ?7
    ?8import?org.springframework.aop.AfterReturningAdvice;
    ?9
    10/**?*//**
    11?*?@author?dragon
    12?*
    13?*/
    14public?class?AfterAdvice?implements?AfterReturningAdvice{
    15????
    16????public?void?afterReturning(Object?returnValue?,Method?method,
    17???????????????????Object[]?args,Object?target)?throws?Throwable{
    18????????System.out.println("这是AfterAdvice类的afterReturning方法.");
    19????}
    20??????
    21
    22}
    23环绕通知:CompareInterceptor.java?1/**?*//**
    ?2?*?
    ?3?*/
    ?4package?com.dragon.Advice;
    ?5
    ?6import?org.aopalliance.intercept.MethodInterceptor;
    ?7import?org.aopalliance.intercept.MethodInvocation;
    ?8
    ?9
    10/**?*//**
    11?*?@author?dragon
    12?*
    13?*/
    14public?class?CompareInterceptor?implements?MethodInterceptor{
    15
    16??????public?Object?invoke(MethodInvocation?invocation)?throws?Throwable{
    17??????????Object?result?=?null;
    18?????????String?stu_name?=?invocation.getArguments()[0].toString();
    19?????????if?(?stu_name.equals("dragon")){
    20?????????????//如果学生是dragon时,执行目标方法,
    21??????????????result=?invocation.proceed();
    22??????????????
    23?????????}?else{
    24?????????????System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");
    25?????????}
    26????????
    27??????????return?result;
    28??????}
    29}
    30
    配置文件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
    ?6<bean?id="beforeAdvice"?class="com.dragon.Advice.BeforeAdvice"></bean>
    ?7<bean?id="afterAdvice"?class="com.dragon.Advice.AfterAdvice"></bean>
    ?8<bean?id="compareInterceptor"?class="com.dragon.Advice.CompareInterceptor"></bean>
    ?9<bean?id="studenttarget"?class="com.dragon.study.Impl.StudentImpl"></bean>
    10
    11<bean?id="student"?class="org.springframework.aop.framework.ProxyFactoryBean">
    12??<property?name="proxyInterfaces">
    13????<value>com.dragon.study.IStudent</value>
    14??</property>
    15??<property?name="interceptorNames">
    16????<list>
    17?????<value>beforeAdvice</value>
    18?????<value>afterAdvice</value>
    19????<value>compareInterceptor</value>??
    20????</list>
    21??</property>
    22??<property?name="target">
    23????<ref?bean="studenttarget"/>
    24??</property>
    25
    26</bean>
    27
    28
    29
    30
    31</beans>? 现在开始写测试类,Test.java
    ?1/**?*//**
    ?2?*?
    ?3?*/
    ?4package?com;
    ?5
    ?6import?org.springframework.context.ApplicationContext;
    ?7import?org.springframework.context.support.FileSystemXmlApplicationContext;
    ?8
    ?9import?com.dragon.study.IStudent;
    10
    11/**?*//**
    12?*?@author?dragon
    13?*
    14?*/
    15public?class?Test?{
    16
    17????/**?*//**
    18?????*?@param?args
    19?????*/
    20????public?static?void?main(String[]?args)?{
    21????????//?TODO?Auto-generated?method?stub
    22??????ApplicationContext?ctx?=?
    23??????????new?FileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");
    24??????
    25??????IStudent?person?=?(IStudent)ctx.getBean("student");
    26??????person.addStudent("dragon");
    27??????
    28//??????person.addStudent("javadragon");
    29????}
    30
    31}
    32
      

  2.   

    Spring Aop中几个概念。
    切面,连接点,处理,切入点,引入,目标对象,Aop代理。这几个概念,不要贴一些概念。用自己的语言形象的来描述一下。1.切面(aspect):要实现的交叉功能,是系统模块化的一个切面或领域。如日志记录。 
    2.连接点:应用程序执行过程中插入切面的地点,可以是方法调用,异常抛出,或者要修改的 
    字段。 
    3.通知:切面的实际实现,他通知系统新的行为。如在日志通知包含了实 
    现日志功能的代码,如向日志文件写日志。通知在连接点插入到应用系统中。 
    4.切入点:定义了通知应该应用在哪些连接点,通知可以应用到AOP框架支持的任何连接点。 
    5.引入:为类添加新方法和属性。 
    6.目标对象:被通知的对象。既可以是你编写的类也可以是第三方类。 
    7.代理:将通知应用到目标对象后创建的对象,应用系统的其他部分不用为了支持代理对象而 
    改变。 
    8.织入:将切面应用到目标对象从而创建一个新代理对象的过程。织入发生在目标 
    对象生命周期的多个点上: 
    编译期:切面在目标对象编译时织入.这需要一个特殊的编译器. 
    类装载期:切面在目标对象被载入JVM时织入.这需要一个特殊的类载入器. 
    运行期:切面在应用系统运行时织入.