xy]; root of factory hierarchy
---------------------
Before Method
--------------------
Book name Effective java
---------------------
Before Method
--------------------
Book URL www.google.cn
----------------------
Before Method
--------------------
2: after advice
在方法运行返回结果后将执行这个 afterReturning方法,创建的这个类必须实现:AfterReturningAdvice接口
[java]
package com.myapp.core.aop.advice;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterMethod implements AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
// TODO Auto-generated method stub
System.out.println("-------------------");
System.out.println("After method ");
}
}
xml配置文件:
[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
beforeMethodBean
afterMethodBean
运行结果如下:
[plain]
三月 20, 2013 2:22:19 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@eb67e8: startup date [Wed Mar 20 14:22:19 CST 2013]; root of context hierarchy
三月 20, 2013 2:22:19 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resource/aop.xml]
三月 20, 2013 2:22:20 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@157985: defining beans [book,beforeMethodBean,afterMethodBean,bookProxy]; root of factory hierarchy
---------------------
Before Method
--------------------
Book name Effective java
-------------------
After method
---------------------
Before Method
--------------------
Book URL www.google.cn
-------------------
After method
----------------------
Before Method
--------------------
3:after throwing advice
当方法执行抛出一个异常后,会执行这个方法,创建一个类实现:ThrowsAdvice接口,创建一个afterThrowing拦截:IllegalArgumentException异常。
类如下:
[java]
package com.myapp.core.aop.advice;
import org.springframework.aop.ThrowsAdvice;
public class ThrowException implements ThrowsAdvice{
public void afterThrowing(IllegalArgumentException e) throws Throwable{
System.out.println("after Throwing Exception");
}
}
xml中配置文件如下:
[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.s