Spring 各种Advice示例
来源:互联网 发布:js禁止浏览器刷新按钮 编辑:程序博客网 时间:2024/06/14 06:08
直接上代码
package com.dada.test.spring.aop.advisor;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice; public class MyAfterAdvice implements AfterReturningAdvice{public void afterReturning(Object result, Method method,Object[] parameter, Object target) throws Throwable {System.out.println("==============after return==============");System.out.println("result:" + result + ";method:" + method+ ";parameter:" + parameter + ";target:" + target);}}
package com.dada.test.spring.aop.advisor;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; }}
package com.dada.test.spring.aop.advisor;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice; public class MyBeforeAdvice implements MethodBeforeAdvice{ public void before(Method method, Object[] parameters, Object target) throws Throwable { System.err.println("==========before==========="); System.out.println(method.getName()); System.out.println(parameters.length); System.out.println(target); }}
package com.dada.test.spring.aop.advisor;import org.springframework.aop.ThrowsAdvice;import com.dada.test.spring.aop.MyException;public class MyThrowsAdvice implements ThrowsAdvice{ public void afterThrowing(MyException e) { // 可以定义多个方法,只要传入的参数是不同异常 System.err.println("MyThrowsAdvice:"+e.toString()); } public void afterThrowing(RuntimeException e) { // 可以定义多个方法,只要传入的参数是不同异常 System.err.print("RuntimeException!"); }}
package com.dada.test.spring.aop.service;public interface IService {String doSth(); void doA();}
package com.dada.test.spring.aop.service;import com.dada.test.spring.aop.MyException;public class MyServiceImpl implements IService{ public String doSth() { System.out.println(this); System.out.println("MyServiceImpl doSth"); return "cindy"; } public void doA() {System.out.println("MyServiceImpl doA"); throw new MyException("111111111"); }}
<?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" class="com.dada.test.spring.aop.advisor.MyBeforeAdvice" /> <bean name="aroundAdvice" class="com.dada.test.spring.aop.advisor.MyAroundAdvice" /> <bean name="afterAdvice" class="com.dada.test.spring.aop.advisor.MyAfterAdvice" /> <bean name="throwsAdvice" class="com.dada.test.spring.aop.advisor.MyThrowsAdvice" /> <bean id="servTarget" class="com.dada.test.spring.aop.service.MyServiceImpl" /> <bean id="invoker" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces" value="com.dada.test.spring.aop.service.IService" /> <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>
package com.dada.test.spring.aop;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.dada.test.spring.aop.service.IService;public class TestSpringAop {public static void main(String[] args) throws Exception{ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "com/dada/test/spring/aop/aop-context.xml" }); IService ser = (IService) ctx.getBean("invoker"); String rs = ser.doSth(); System.out.println("rs="+rs.toString()); try { ser.doA(); } catch (Throwable t) { t.printStackTrace(); } }}
运行结果如下:
2013-7-18 23:21:06 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14ed9ff: display name [org.springframework.context.support.ClassPathXmlApplicationContext@14ed9ff]; startup date [Thu Jul 18 23:21:06 CST 2013]; root of context hierarchy
2013-7-18 23:21:06 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/dada/test/spring/aop/aop-context.xml]
2013-7-18 23:21:07 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@14ed9ff]:org.springframework.beans.factory.support.DefaultListableBeanFactory@12152e6
2013-7-18 23:21:07 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@12152e6: defining beans [beforeAdvice,aroundAdvice,afterAdvice,throwsAdvice,servTarget,invoker]; root of factory hierarchy
2013-7-18 23:21:07 org.springframework.aop.framework.ProxyFactoryBean getObject
警告: Using non-singleton proxies with singleton targets is often undesirable.Enable prototype proxies by setting the 'targetName' property.
==========before===========
doSth
0
com.dada.test.spring.aop.service.MyServiceImpl@6f7ce9
============around before==========
com.dada.test.spring.aop.service.MyServiceImpl@6f7ce9
MyServiceImpl doSth
==============after return==============
result:cindy;method:public abstract java.lang.String com.dada.test.spring.aop.service.IService.doSth();parameter:[Ljava.lang.Object;@1dfafd1;target:com.dada.test.spring.aop.service.MyServiceImpl@6f7ce9
==============after return==============
result:cindy;method:public abstract java.lang.String com.dada.test.spring.aop.service.IService.doSth();parameter:[Ljava.lang.Object;@8fce95;target:com.dada.test.spring.aop.service.MyServiceImpl@6f7ce9
============around after==========
rs=cindy
==========before===========
doA
0
com.dada.test.spring.aop.service.MyServiceImpl@6f7ce9
============around before==========
MyServiceImpl doA
MyThrowsAdvice:com.dada.test.spring.aop.MyException: 111111111
com.dada.test.spring.aop.MyException: 111111111
at com.dada.test.spring.aop.service.MyServiceImpl.doA(MyServiceImpl.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:126)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at com.dada.test.spring.aop.advisor.MyAroundAdvice.invoke(MyAroundAdvice.java:15)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy0.doA(Unknown Source)
at com.dada.test.spring.aop.TestSpringAop.main(TestSpringAop.java:21)
after return出现了2次请注意,思考下是什么原因
- Spring 各种Advice示例
- Advice Types in Spring
- Spring-Throw Advice异常
- spring advice 分类
- Spring AOP tx:advice
- spring advice&pointcut&advisor
- Spring AOP之Advice
- Spring <tx:advice/>
- Spring之Advice小结
- spring的通知(advice)
- Spring创建Advice
- Spring.NET sample -> Around Advice
- Spring中的Advisor,Advice,Pointcut
- Spring-基于Annotation的Advice
- spring tx:advice事务配置
- Spring AOP Example – Advice
- Spring <tx:advice>事务配置
- Spring AOP Example – Advice
- 推荐近年来印象最深的几本书 .
- 使用Excel背单词-高效-简单
- spring MVC session传递model
- 关于网络数据传输大小端字节序的转换
- java JDK7安装后指定环境变量失败的原因
- Spring 各种Advice示例
- 推荐个软件:wxHexEditor,基于GPL协议,十六进制编辑器,支持存储设备底层读写!
- Debian系统常用命令(转)
- JavaScript实现复制功能
- python之 break语句和continue语句及PASS语句
- 黑马程序员-java基础(一)
- 小小菜鸟 实习生的苦恼
- CentOS修改文本界面分辨率
- 持续集成之路——数据访问层单元测试遇到的问题