设计方式-proxy
设计模式-proxySpring的Aop和?Struts个拦截器,动态代理模式就是执行某方法,之前或之后干些什么!!!首先是简
设计模式-proxy
Spring的Aop和?Struts个拦截器,动态代理模式就是执行某方法,之前或之后干些什么!!!
首先是简单的Aspect接口
?
? ? ? public?interface?Student?{ ??????public?void?sayHello(); ?? } ????import?org.aspect.Aspect; ?? ??public?class?StudentAspectOne?implements?Aspect?{ ?? ??????@Override?? ????public?void?doAfter()?{ ?? ????????System.out.println("do?After?One"); ?? ???????? ??????} ????????@Override?? ????public?void?doBefore()?{ ?? ????????System.out.println("do?Before?One"); ?? ???????? ??????} ????} ????import?org.aspect.Aspect; ?? ????public?class?StudentAspectTwo?implements?Aspect{ ?? ????@Override?? ????public?void?doAfter()?{ ?? ????????System.out.println("do?After?Two"); ?? ???????? ??????} ????????@Override?? ????public?void?doBefore()?{ ?? ????????System.out.println("do?Before?Two"); ?? ???????? ??????} ??} ????public?class?StudentImpl?implements?Student?{ ?? ??????@Override?? ????public?void?sayHello()?{ ?? ????????System.out.println("Student"); ?? ????} ????} ????import?java.util.ArrayList; ?? import?java.util.List; ?? ??import?org.aspect.Aspect; ?? import?org.aspect.DynamicProxyFactory; ?? ??public?class?Test?{ ?? ??????public?static?void?main(String[]?args)?{ ?? ???????? ??????????List<Aspect>?aspectList?=?new?ArrayList<Aspect>(); ?? ???????? ??????????aspectList.add(new?StudentAspectOne()); ?? ????????aspectList.add(new?StudentAspectTwo()); ?? ????????Student?s?=?(Student)DynamicProxyFactory.newInstance(new?StudentImpl(),?aspectList); ?? ????????s.sayHello(); ??????} ????}??
执行Test,控制台返回
do Before One
do Before Two
Student
do After Two
do After One