Chinaunix首页 | 论坛 | 博客
  • 博客访问: 203210
  • 博文数量: 73
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 750
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 18:32
文章分类

全部博文(73)

文章存档

2009年(1)

2008年(72)

我的朋友

分类: Java

2008-03-22 19:41:10

  IntroductionDemo Project实现在Some类中“添加”额外的行为。

package com.biaoflying;

public interface ISome {
    public void doSome();
}

package com.biaoflying;

public class Some implements ISome {
    public void doSome(){
        System.out.println("原来的职责。。。");
    }
}

#添加额外行为的接口&类
package com.biaoflying;

public interface IOther {
    public void doOther();
}
package com.biaoflying;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.IntroductionInterceptor;

public class OtherIntroduction implements IOther,
             IntroductionInterceptor {

    public boolean implementsInterface(Class clazz){
        return clazz.isAssignableFrom(IOther.class);
    }
   
    public Object invoke(MethodInvocation methodInvocation)
            throws Throwable{
        if(implementsInterface(
                methodInvocation.getMethod().getDeclaringClass())){
            return methodInvocation.getMethod().
                invoke(this,methodInvocation.getArguments());
        }else{
            return methodInvocation.proceed();
        }
    }
   
    public void doOther() {
        // TODO Auto-generated method stub
        System.out.println("增加的职责。。。");
    }
}

package com.biaoflying;

import org.springframework.context.ApplicationContext;
import org.springframework.context.
       support.FileSystemXmlApplicationContext;

public class SpringAOPDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context=
            new FileSystemXmlApplicationContext("beans-config.xml");
        
        ISome some=(ISome)context.getBean("helloProxy");
        some.doSome();
        
        ((IOther)some).doOther();
    }
}


 "">
 

    
    
            class="com.biaoflying.OtherIntroduction"/>
            class="org.springframework.aop.
          support.DefaultIntroductionAdvisor">
        
            
        

        
            com.biaoflying.IOther
        

    

    
            class="org.springframework.aop
          .framework.ProxyFactoryBean">
        
            com.biaoflying.ISome
        

        
            
        

        
            
                otherAdvisor
            

        

    


#输出:
原来的职责。。。
增加的职责。。。

-----------------------------------------------------------------------------------

  使用DelegatingIntroductionInterceptor,这是由spring框架提供的一个IntroductionInterceptor实现类。

#Target的接口及实现类
package com.biaoflying;

public interface ISome {
    public void setSome(String some);
    public String getSome();
}
#Some.java
package com.biaoflying;

public class Some implements ISome {
    private String some;
    @Override
    public String getSome() {
        // TODO Auto-generated method stub
        return some;
    }

    @Override
    public void setSome(String some) {
        // TODO Auto-generated method stub
        this.some=some;
    }

}

#"额外行为"接口及其实现
package com.biaoflying;

public interface ILockable {
    public void lock();
    public void unlock();
    public boolean isLocked();
}
#LockIntroduction.java
package com.biaoflying;

import org.springframework.aop.support.*;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.AopConfigException;

public class LockIntroduction
        extends DelegatingIntroductionInterceptor
        implements ILockable{

    private boolean locked;
   
    public Object invoke(MethodInvocation invocation)
        throws Throwable{
        if(isLocked()&&
                invocation.getMethod()
                .getName().indexOf("set")==0){
            throw new AopConfigException("物件被锁定!");
        }
        return super.invoke(invocation);
    }
   
    public boolean isLocked() {
        // TODO Auto-generated method stub
        return locked;
    }

    @Override
    public void lock() {
        // TODO Auto-generated method stub
        locked=true;
    }

    @Override
    public void unlock() {
        // TODO Auto-generated method stub
        locked=false;
    }
}

#主程序
package com.biaoflying;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.
            FileSystemXmlApplicationContext;

public class SpringAOPDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context=
            new FileSystemXmlApplicationContext("beans-config.xml");
        ISome some=
            (ISome)context.getBean("helloProxy");
        some.setSome("abio");
        System.out.println(some.getSome());
        
        try{
            ((ILockable)some).lock();
            some.setSome("biaoflying");
            System.out.println(some.getSome());
        }catch(Throwable e){
            e.printStackTrace();
        }
        ((ILockable)some).unlock();
        some.setSome("momor");
        System.out.println(some.getSome());
    }
}

#配置文件

 "">
 

   
   
            class="com.biaoflying.LockIntroduction"/>
            class="org.springframework.
          aop.support.DefaultIntroductionAdvisor">
       
           
       

       
            com.biaoflying.ILockable
       

   

   
            class="org.springframework.
          aop.framework.ProxyFactoryBean">
       
            com.biaoflying.ISome
       

       
           
       

       
           
                lockAdvisor
           

       

   



#输出:
abio
org.springframework.aop.framework.AopConfigException: 物件被锁定!.........
...............
momor

阅读(894) | 评论(0) | 转发(0) |
0

上一篇:Spring之旅三 AOP(续)

下一篇:Spring之旅四 DAO

给主人留下些什么吧!~~