分类: Java
2008-03-22 19:41:10
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(); } } |
#输出: 原来的职责。。。 增加的职责。。。 |
#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()); } } |
#输出: abio org.springframework.aop.framework.AopConfigException: 物件被锁定!......... ............... momor |