Spring Aop入门文档格式.docx

上传人:b****4 文档编号:18022529 上传时间:2022-12-12 格式:DOCX 页数:15 大小:23.36KB
下载 相关 举报
Spring Aop入门文档格式.docx_第1页
第1页 / 共15页
Spring Aop入门文档格式.docx_第2页
第2页 / 共15页
Spring Aop入门文档格式.docx_第3页
第3页 / 共15页
Spring Aop入门文档格式.docx_第4页
第4页 / 共15页
Spring Aop入门文档格式.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

Spring Aop入门文档格式.docx

《Spring Aop入门文档格式.docx》由会员分享,可在线阅读,更多相关《Spring Aop入门文档格式.docx(15页珍藏版)》请在冰豆网上搜索。

Spring Aop入门文档格式.docx

前置通知、后置通知、环绕通知、异常通知实现

我以Spring 

InAction 

提供的例子进行二次改造,完成我们自己的流程。

业务流程很简单,顾客到商店买东西,店员根据顾客的需要给于顾客提供服务。

实现方法前插入,方法后插入,环绕,异常四种。

代码:

 

建立一个用户类;

publicclassCustomer 

{

privateStringname="

悠~游!

"

;

publicStringgetName(){

returnname;

}

}

三个产品

publicclassCheese{

publicStringtoString(){

return"

奶酪!

publicclassPepper{

胡椒粉!

publicclassSquish{

果酱!

建立一个接口;

publicinterfaceKwikEMart{

SquishbuySquish(Customercustomer)throwsKwikEMartException;

PepperbuyPepper(Customercustomer)throwsKwikEMartException;

CheesebuyCheese(Customercustomer)throwsKwikEMartException;

实现这个接口,我们实现三个方法,买奶酪,买胡椒粉,买果酱;

publicclassApuKwikEMartimplementsKwikEMart{

privatebooleancheeseIsEmpty=false;

privatebooleanpepperIsEmpty=false;

privatebooleansquishIsEmpty=false;

publicCheesebuyCheese(Customercustomer)throwsNoMoreCheeseException{

if(cheeseIsEmpty){

thrownewNoMoreCheeseException();

Cheeses=newCheese();

System.out.println("

--我想买:

+s);

returns;

publicPepperbuyPepper(Customercustomer)throwsNoMorePepperException{

if(pepperIsEmpty){

thrownewNoMorePepperException();

Peppers=newPepper();

publicSquishbuySquish(Customercustomer)throwsNoMoreSquishException{

if(squishIsEmpty){

thrownewNoMoreSquishException();

Squishs=newSquish();

publicvoidsetCheeseIsEmpty(booleancheeseIsEmpty){

this.cheeseIsEmpty=cheeseIsEmpty;

publicvoidsetPepperIsEmpty(booleanpepperIsEmpty){

this.pepperIsEmpty=pepperIsEmpty;

publicvoidsetSquishIsEmpty(booleansquishIsEmpty){

this.squishIsEmpty=squishIsEmpty;

环绕通知的实现,必须实现invoke方法,通过调用invoke.proceed()手工调用对象方法:

publicclassOnePerCustomerInterceptorimplementsMethodInterceptor{

privateSetcustomers=newHashSet();

publicObjectinvoke(MethodInvocationinvoke)throwsThrowable{

Customercustomer=(Customer)invoke.getArguments()[0];

if(customers.contains(customer)){

thrownewKwikEMartException("

Onepercustomer."

);

店员:

+customer.getName()+"

CanIhelpyou?

Objectsquishee=invoke.proceed();

//手工调用对象方法;

OK!

"

+customer.getName()+"

.giveyou!

);

customers.add(squishee);

returnsquishee;

前置通知的实现;

publicclassWelcomeAdviceimplementsMethodBeforeAdvice{

publicvoidbefore(Methodmethod,Object[]args,Objecttarget)throwsThrowable{

Customercustomer=(Customer)args[0];

:

Hello"

.Howareyoudoing?

后置通知实现;

publicclassThankYouAdviceimplementsAfterReturningAdvice{

publicvoidafterReturning(ObjectreturnValue,Methodmethod,Object[]args,Objecttarget)throwsThrowable{

Thankyou"

.Comeagain!

系统异常处理通知实现;

publicclassKwikEmartExceptionAdviceimplementsThrowsAdvice{

publicvoidafterThrowing(NoMoreSquishExceptione){

系统:

NoMoreSquisheesException异常截获了:

+e.getMessage());

publicvoidafterThrowing(NoMoreCheeseExceptione){

NoMoreCheeseException异常截获了:

publicvoidafterThrowing(NoMorePepperExceptione){

NoMorePepperException异常截获了:

自定义的异常接口;

publicclassKwikEMartExceptionextendsException{

privatestaticfinallongserialVersionUID=-3962577696326432053L;

StringretValue="

KwikEMartException 

异常!

publicKwikEMartException(Stringname){

retValue=name;

publicKwikEMartException(){

publicStringgetMessage(){

returnretValue;

没有更多的奶酪异常;

publicclassNoMoreCheeseExceptionextendsKwikEMartException{

privatestaticfinallongserialVersionUID=-3961123496322432053L;

NoMoreCheeseException 

publicNoMoreCheeseException(){

super();

publicNoMoreCheeseException(Stringname){

super(name);

没有更多的胡椒粉异常;

publicclassNoMorePepperExceptionextendsKwikEMartException{

privatestaticfinallongserialVersionUID=-3961234696322432053L;

NoMorePepperException 

publicNoMorePepperException(){

publicNoMorePepperException(Stringname){

没有更多的果酱异常;

publicclassNoMoreSquishExceptionextendsKwikEMartException{

privatestaticfinallongserialVersionUID=-3121234696322432053L;

NoMoreSquishException 

publicNoMoreSquishException(){

publicNoMoreSquishException(Stringname){

运行实例类;

publicclassRunDemo{

publicstaticvoidkwikEMart(){

ClassPathXmlApplicationContextcontext=newClassPathXmlApplicationContext("

demo/kwikemart.xml"

//如果你想通过类来引用这个的话,就要用到CGLIB.jar了,同时在代理工厂里面设置:

//<

propertyname="

proxyTargetClass"

value="

true"

/>

KwikEMartakem=(KwikEMart)context.getBean("

kwikEMart"

try{

akem.buySquish(newCustomer());

akem.buyPepper(newCustomer());

akem.buyCheese(newCustomer());

}catch(KwikEMartExceptione){

//异常已经被截获了,不信你看控制台!

~;

publicstaticvoidmain(String[]args){

kwikEMart();

Xml文件配置:

<

?

xmlversion="

1.0"

encoding="

UTF-8"

>

!

DOCTYPEbeansPUBLIC"

-//SPRING//DTDBEAN//EN"

http:

//www.springframework.org/dtd/spring-beans.dtd"

beans>

<

beanid="

kwikEMartTarget"

class="

demo.ApuKwikEMart"

--

把这里注释去掉的话,程序调用的时候测试异常通知;

cheeseIsEmpty"

value>

true<

/value>

/property>

pepperIsEmpty"

squishIsEmpty"

-->

/bean>

-- 

方法调用前通知 

-->

welcomeAdvice"

demo.advice.WelcomeAdvice"

方法调用后通知-->

thankYouAdvice"

demo.advice.ThankYouAdvice"

环绕调用通知 

onePerCustomerInterceptor"

demo.advice.OnePerCustomerInterceptor"

异常调用通知 

kwikEmartExceptionAdvice"

demo.advice.KwikEmartExceptionAdvice"

org.springframework.aop.framework.ProxyFactoryBean"

proxyInterfaces"

demo.KwikEMart"

interceptorNames"

list>

welcomeAdvice<

thankYouAdvice<

onePerCustomerInterceptor<

kwikEmartExceptionAdvice<

/list>

target"

refbean="

/beans>

这个例子东西很多,不过每个类的代码都不大。

如果你对org.springframework.aop.framework.ProxyFactoryBean不是很了解的话可以看我下篇尾处的介绍。

读清楚之后,我们运行RunDemo类,查看控制台结果,如下:

Hello悠~游!

悠~游!

Thankyou悠~游!

我们将kwikEMartTarget里面的

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1