Spring中的各种Advice的使用原创 spring advice 配置

Before advice

package com.aop.advice;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MyBeforeAdvice implementsMethodBeforeAdvice

{

public void before(Method method, Object[]parameters, Object target) throws Throwable

{

System.err.println("==========before===========");

System.out.println(method.getName());

System.out.println(parameters);

System.out.println(target);

}

}

Around advice

package com.aop.advice;

import java.util.HashSet;

import java.util.Set;

import org.aopalliance.intercept.MethodInterceptor;

import org.aopalliance.intercept.MethodInvocation;

public class MyAroundAdvice implements MethodInterceptor

{

private Set set=new HashSet();

public Object invoke(MethodInvocation method) throws Throwable

{

set.add(method);

System.out.println("============around before==========");

Object result=method.proceed();

System.out.println("============around after==========");

return result;

}

}

After return advice

package com.aop.advice;

import java.lang.reflect.Method;

importorg.springframework.aop.AfterReturningAdvice;

public class MyAfterAdvice implements AfterReturningAdvice

{

public void afterReturning(Object result, Method method,Object[] parameter, Object target) throws Throwable

{

System.err.println("==============afterreturn==============");

System.out.println("result:"+result+";method:"+method+";parameter:"+parameter+";target:"+target);

}

}

Throwadvice

package com.aop.advice;

import org.springframework.aop.ThrowsAdvice;

import com.aop.MyException;

public class MyThrowsAdvice implements ThrowsAdvice

{

public void afterThrowing(MyException e)

{ // 可以定义多个方法,只要传入的参数是不同异常

System.err.println(e.toString());

}

public void afterThrowing(RuntimeException e)

{

// 可以定义多个方法,只要传入的参数是不同异常

System.err.print("RuntimeException!");

}

}

My exception

package com.aop;

public class MyException extends RuntimeException

{

public MyException(String msg)

{

super(msg);

}

public MyException()

{

}

public MyException(String arg0, Throwable arg1)

{

super(arg0, arg1);

}

public MyException(Throwable arg0)

{

super(arg0);

}

}

Service接口

package com.aop;

public interface IServ

{

String[]doSth();

void doA();

}

Service实现

package com.aop;

public class Serv implements IServ

{

public String[] doSth()

{

Spring中的各种Advice的使用【原创】 spring advice 配置

System.out.println(this);

return new String[]{"cindy","guoguo","xixi"};

}

public voiddoA()

{

throw new MyException("111111111");

}

}

Context文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">

<beans default-lazy-init="true">

<bean name="beforeAdvice"/>

<bean name="aroundAdvice"/>

<bean name="afterAdvice"/>

<bean name="throwsAdvice"/>

<bean id="servTarget" />

<bean id="invoker"

>

<property name="proxyInterfaces" value="com.aop.IServ" />

<property name="target" ref="servTarget" />

<property name="singleton" value="false" />

<property name="interceptorNames">

<list>

<value>beforeAdvice</value>

<value>aroundAdvice</value>

<value>afterAdvice</value>

<value>afterAdvice</value>

<value>throwsAdvice</value>

</list>

</property>

</bean>

</beans>

测试类

public static void main(String[] args)

{

ClassPathXmlApplicationContext ctx = newClassPathXmlApplicationContext(new String[]

{

"com/aop/aop-context.xml"

});

IServ ser = (IServ) ctx.getBean("invoker");

ser.doSth();

try

{

ser.doA();

}

catch (Throwable t)

{

t.printStackTrace();

}

}

  

爱华网本文地址 » http://www.aihuau.com/a/25101014/205536.html

更多阅读

怎样使用百度工具栏保存网页中的视频、音频? 保存网页音频

很多网友为了保存网页上的音视频伤透了,脑筋,安装了各种各样的软件,甚至用上了屏幕录像。其实完全不用这么麻烦,只需用一个小插件就能轻松下载。怎样使用百度工具栏保存网页中的视频、音频?——工具/原料IE浏览器 百度工具栏怎样使用百度

MFC中进度条控件的使用方法 mfc进度条控件使用

MFC中进度条控件的使用方法——简介进度条控件是程序开发中基础控件之一,常用于显示程序的进度。在进行程序安装、文件传输时经常用到。其用法也比较简单固定。今天就和大家分享一下其简单的使用方法吧。^_^MFC中进度条控件的使用方

excel中round函数的使用方法 isodd函数的使用方法

excel中round函数的使用方法——简介不少朋友都会问在excel中round函数怎么用,作为使用频率较高函数之一,本文就介绍一下round函数的使用方法。excel中round函数的使用方法——工具/原料office excelexcel中round函数的使用方法——

如何使用win7中的画图功能 画图功能怎么使用

如何使用win7中的画图功能——简介多人甚至已经遗忘了Win7画图程序在哪里,不妨再多啰嗦几句。相比于XP等老系统,Win7的画图工具还是改进了不少,比如类菜单似于Office的Ribbon风格界面。很显然,画图程序的主要功能就是图片处理,一些简单的

ps中套索工具怎么使用的方法 ps磁性套索工具的使用

ps中套索工具怎么使用的方法——简介在Photoshop中,如何利用套索工具来建立选区,处理成自己想要的图片呢?这里通过一个实例,来讲解下套索工具的使用方法。ps中套索工具怎么使用的方法——工具/原料

声明:《Spring中的各种Advice的使用原创 spring advice 配置》为网友奇迹少年分享!如侵犯到您的合法权益请联系我们删除