1、Spring Aop入门Spring-Aop入门AOP全名Aspect-oriented programming。Springframework是很有前途的AOP技术。作为一种非侵略性的,轻型的AOP framework,你无需使用预编译器或其他的元标签,便可以在Java程序中使用它。这意味着开发团队里只需一人要对付AOP framework,其他人还是像往常一样编程。关键性概念:1)Advice是代码的具体实现,例如一个实现日志记录的代码。2)Pointcut是在将Advice插入到程序的条件。3)advisor是把pointcut和advice的组合在一起装配器。图例:你的程序可能如上,现
2、在要在三个流程上同时加入日志控制和权限控制,如下:你的程序可能如上,现在要在三个流程上同时加入日志控制和权限控制,如下:其中拿日志为例,日志控制和流程之间的穿插点处叫做连接点(Joinpoint),而Advice就是我们日志处理的具体代码,Pointcut就是定义一个规则,对三个和业务有关的连接点进行过滤和匹配(例如我们对于业务1不做日志处理)。Advisor就是将符合的规则的剩下的两个连接点和具体的日志记录代码组合在一起。三.Spring-Aop前置通知、后置通知、环绕通知、异常通知实现我以SpringIn Action提供的例子进行二次改造,完成我们自己的流程。业务流程很简单,顾客到商店买
3、东西,店员根据顾客的需要给于顾客提供服务。实现方法前插入,方法后插入,环绕,异常四种。代码: 建立一个用户类;public class Customer private String name = 悠游!; public String getName() return name; 三个产品public class Cheese public String toString() return 奶酪!; public class Pepper public String toString() return 胡椒粉!; public class Squish public String toStri
4、ng() return 果酱!; 建立一个接口;public interface KwikEMart Squish buySquish(Customer customer) throws KwikEMartException; Pepper buyPepper(Customer customer) throws KwikEMartException; Cheese buyCheese(Customer customer) throws KwikEMartException;实现这个接口,我们实现三个方法,买奶酪,买胡椒粉,买果酱;public class ApuKwikEMart implem
5、ents KwikEMart private boolean cheeseIsEmpty = false; private boolean pepperIsEmpty = false; private boolean squishIsEmpty = false; public Cheese buyCheese(Customer customer) throws NoMoreCheeseException if (cheeseIsEmpty) throw new NoMoreCheeseException(); Cheese s = new Cheese(); System.out.printl
6、n(-我想买: + s); return s; public Pepper buyPepper(Customer customer) throws NoMorePepperException if (pepperIsEmpty) throw new NoMorePepperException(); Pepper s = new Pepper(); System.out.println(-我想买: + s); return s; public Squish buySquish(Customer customer) throws NoMoreSquishException if (squishIs
7、Empty) throw new NoMoreSquishException(); Squish s = new Squish(); System.out.println(-我想买: + s); return s; public void setCheeseIsEmpty(boolean cheeseIsEmpty) this.cheeseIsEmpty = cheeseIsEmpty; public void setPepperIsEmpty(boolean pepperIsEmpty) this.pepperIsEmpty = pepperIsEmpty; public void setS
8、quishIsEmpty(boolean squishIsEmpty) this.squishIsEmpty = squishIsEmpty; 环绕通知的实现,必须实现invoke方法,通过调用invoke.proceed()手工调用对象方法:public class OnePerCustomerInterceptor implements MethodInterceptor private Set customers=new HashSet(); public Object invoke(MethodInvocation invoke) throws Throwable Customer c
9、ustomer=(Customer)invoke.getArguments()0; if(customers.contains(customer) throw new KwikEMartException(One per customer.); System.out.println(店员:+customer.getName() + ,Can I help you ?); Object squishee=invoke.proceed(); /手工调用对象方法; System.out.println(店员:OK! + customer.getName() + .give you! ); custo
10、mers.add(squishee); return squishee; 前置通知的实现;public class WelcomeAdvice implements MethodBeforeAdvice public void before(Method method, Object args, Object target) throws Throwable Customer customer = (Customer) args0; System.out.println(店员:Hello + customer.getName() + . How are you doing?); public
11、class Customer private String name = 悠游!; public String getName() return name; 后置通知实现;public class ThankYouAdvice implements AfterReturningAdvice public void afterReturning(Object returnValue, Method method, Object args, Object target) throws Throwable Customer customer = (Customer) args0; System.ou
12、t.println(店员:Thank you + customer.getName() + . Come again! ); 系统异常处理通知实现;public class KwikEmartExceptionAdvice implements ThrowsAdvice public void afterThrowing(NoMoreSquishException e) System.out.println(系统:NoMoreSquisheesException异常截获了: + e.getMessage(); public void afterThrowing(NoMoreCheeseExce
13、ption e) System.out.println(系统:NoMoreCheeseException异常截获了: + e.getMessage(); public void afterThrowing(NoMorePepperException e) System.out.println(系统:NoMorePepperException异常截获了: + e.getMessage(); 自定义的异常接口;public class KwikEMartException extends Exception private static final long serialVersionUID =
14、-3962577696326432053L; String retValue = KwikEMartException异常!; public KwikEMartException(String name) retValue = name; public KwikEMartException() public String getMessage() return retValue; 没有更多的奶酪异常;public class NoMoreCheeseException extends KwikEMartException private static final long serialVers
15、ionUID = -3961123496322432053L; String retValue = NoMoreCheeseException异常!; public NoMoreCheeseException() super(); public NoMoreCheeseException(String name) super(name); public String getMessage() return retValue; 没有更多的胡椒粉异常;public class NoMorePepperException extends KwikEMartException private stat
16、ic final long serialVersionUID = -3961234696322432053L; String retValue = NoMorePepperException异常!; public NoMorePepperException() super(); public NoMorePepperException(String name) super(name); public String getMessage() return retValue; 没有更多的果酱异常;public class NoMoreSquishException extends KwikEMar
17、tException private static final long serialVersionUID = -3121234696322432053L; String retValue = NoMoreSquishException异常!; public NoMoreSquishException() super(); public NoMoreSquishException(String name) super(name); public String getMessage() return retValue; 运行实例类;public class RunDemo public stat
18、ic void kwikEMart() ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(demo/kwikemart.xml); /如果你想通过类来引用这个的话,就要用到CGLIB.jar了,同时在代理工厂里面设置: / KwikEMart akem = (KwikEMart) context.getBean(kwikEMart); try akem.buySquish(new Customer(); akem.buyPepper(new Customer(); akem.buyCheese
19、(new Customer(); catch (KwikEMartException e) /异常已经被截获了,不信你看控制台!; public static void main(String args) kwikEMart(); Xml文件配置: !- 把这里注释去掉的话,程序调用的时候测试异常通知; true true true - welcomeAdvice thankYouAdvice onePerCustomerInterceptor kwikEmartExceptionAdvice 这个例子东西很多,不过每个类的代码都不大。如果你对org.springframework.aop.f
20、ramework.ProxyFactoryBean不是很了解的话可以看我下篇尾处的介绍。读清楚之后,我们运行RunDemo 类,查看控制台结果,如下:店员:Hello 悠游! . How are you doing?店员:悠游! ,Can I help you ?-我想买:果酱!店员:OK! 悠游!.give you!店员:Thank you 悠游! . Come again!店员:Hello 悠游! . How are you doing?店员:悠游! ,Can I help you ?-我想买:胡椒粉!店员:OK! 悠游!.give you!店员:Thank you 悠游! . Come again!店员:Hello 悠游! . How are you doing?店员:悠游! ,Can I help you ?-我想买:奶酪!店员:OK! 悠游!.give you!店员:Thank you 悠游! . Come again!我们将kwikEMartTarget里面的
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1