分类: Java
2008-03-22 18:46:48
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(); } } |
#输出: 信息: 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 |