Before Advice:
package com.hwp.aop.adviceDemo;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdviceDemo implements MethodBeforeAdvice {

public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("在方法運行前,先運行");
}
}
After Advice:
package com.hwp.aop.adviceDemo;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class AfterAdviceDemo implements AfterReturningAdvice{

public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("方法執行后
.");
}
}
Round Advice:
package com.hwp.aop.adviceDemo;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AroundAdviceDemo implements MethodInterceptor {

public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("在round方法里,方法開始前
..");
Object result = null;
try {
result = arg0.proceed();
} finally {
System.out.println("在round方法里,方法結束后");
}
return result;
}

}
Throw Advice:
package com.hwp.aop.adviceDemo;

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;

public class ThowAdviceDemo implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] args, Object target,
Throwable subclass) {
System.out.println("異常拋出后

..");
}

}
beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.0.xsd">
<bean id="logBeforeAdvice"
class="com.hwp.aop.adviceDemo.BeforeAdviceDemo" />
<bean id="logAfterAdvice"
class="com.hwp.aop.adviceDemo.AfterAdviceDemo" />
<bean id="logRoundAdvice"
class="com.hwp.aop.adviceDemo.AroundAdviceDemo" />
<bean id="throwAdvice"
class="com.hwp.aop.adviceDemo.ThowAdviceDemo" />
<bean id="helloSpeaker" class="com.hwp.aop.adviceDemo.HelloSpeaker" />
<bean id="helloProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"
value="com.hwp.aop.adviceDemo.IHello" />
<property name="target" ref="helloSpeaker" />
<property name="interceptorNames">
<list>
<value>logBeforeAdvice</value>
<value>logAfterAdvice</value>
<value>logRoundAdvice</value>
<value>throwAdvice</value>
</list>
</property>
</bean>
</beans>
Demo:
package com.hwp.aop.adviceDemo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAOPDemo {
public static void main(String[] args){
ApplicationContext context =
new ClassPathXmlApplicationContext("beans-beforeAdvice.xml");
IHello helloProxy = (IHello) context.getBean("helloProxy");
try{
helloProxy.hello("惠萬鵬");
}catch(Exception e){
//e.printStackTrace();
}
}
}
收工....




















































































































