您的位置:首页 > 编程语言 > Java开发

[转]Spring 各种Advice示例

2015-07-19 10:16 465 查看
直接上代码
[java] view plaincopyprint?
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.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);
}

}


[java] view plaincopyprint?
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.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;
}
}


[java] view plaincopyprint?
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 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);
}
}


[java] view plaincopyprint?
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.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!");
}
}


[java] view plaincopyprint?
package com.dada.test.spring.aop.service;

public interface IService {

String doSth();

void doA();

}

package com.dada.test.spring.aop.service;

public interface IService {

String doSth();
void doA();
}


[java] view plaincopyprint?
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");

}

}

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");
}

}


[html] view plaincopyprint?
<?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>

<?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>


[java] view plaincopyprint?
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();

}

}

}

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次请注意,思考下是什么原因
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: