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

全部博文(73)

文章存档

2009年(1)

2008年(72)

我的朋友

分类: Java

2008-03-22 18:46:48

  动态Pointcut,ControlFlowPointcut。以下Project,表示当some类调用了Target上的某个方法时,会加入某个advice。

package com.biaoflying;

public interface IHello {
    public void helloNewbie(String name);
    public void helloMaster(String name);
}

package com.biaoflying;

public class HelloSpeaker implements IHello {
    public void helloNewbie(String name){
        System.out.println("Hello "+name+" newbie");
    }
    public void helloMaster(String name){
        System.out.println("Hello "+name+" will be master");
    }
}

package com.biaoflying;

import java.lang.reflect.Method;
import java.util.logging.*;
import org.springframework.aop.MethodBeforeAdvice;

public class LogBeforeAdvice
        implements MethodBeforeAdvice{
    private Logger logger=
        Logger.getLogger(this.getClass().getName());
    
    public void before(Method method,Object[] args,
            Object target)throws Throwable{
        logger.log(Level.INFO,"Method starts..."+method);
    }
}

#调用目标对象方法的类Some.java,这里使用到了#ApplicationContextAware接口。
package com.biaoflying;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Some implements ApplicationContextAware{
    private IHello helloProxy;
    
    public void setApplicationContext(
            ApplicationContext context)
            throws BeansException{
        helloProxy=(IHello)context.getBean("helloProxy");
    }
    
    public void helloEverybody(){
        helloProxy.helloNewbie("abio");
        helloProxy.helloMaster("biaoflying");
    }
}

package com.biaoflying;

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

public class SpringAOPDemo {

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


 "">
 

            class="com.biaoflying.LogBeforeAdvice"/>
    
    
            class="org.springframework.aop.
          support.ControlFlowPointcut">
        
            com.biaoflying.Some
        

    

            class="org.springframework.aop.
          support.DefaultPointcutAdvisor">
        
            
        

        
            
        

    

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

        
            
        

        
            
                helloAdvisor
            

        

    


#输出:
信息: Method starts...public abstract void com.biaoflying.IHello.helloNewbie(java.lang.String)
2008-3-22 18:46:26 com.biaoflying.LogBeforeAdvice before
信息: Method starts...public abstract void com.biaoflying.IHello.helloMaster(java.lang.String)
Hello abio newbie
Hello biaoflying will be master





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

上一篇:Spring之旅二

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

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