锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
public interface HelloI {
聽public String sayHello(String firstName,String lastName)throws Exception;
聽}
綾諱慨鏀逛負
package net.blogjava.dodoma.spring.aop;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Hello implements HelloI {
聽protected static final Log log=LogFactory.getLog(Hello.class);
聽private String msg;
聽public Hello(){}
聽public Hello(String msg){
聽聽this.msg=msg;
聽}
聽
聽public String getMsg() {
聽聽return msg;
聽}
聽public void setMsg(String msg) {
聽聽this.msg = msg;
聽}
聽public String sayHello(String firstName, String lastName) throws Exception{
聽聽// TODO Auto-generated method stub
聽聽log.info("in the class "+this.getClass().getName()+"'s method sayHello()");
聽 throw new Exception("here is a exception !");
聽聽return (msg+" "+firstName+" "+lastName);
聽}
}
package net.blogjava.dodoma.spring.aop;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.ThrowsAdvice;
public class LogThrowAdvice implements ThrowsAdvice {
聽protected static final Log log = LogFactory.getLog(LogThrowAdvice.class);
聽public void afterThrowing(Exception e)throws Throwable{
聽聽log.info("in the class "+this.getClass().getName()+"'s method afterThrowing()");
聽聽log.info("the exception is "+e.getMessage());
聽}
}
package net.blogjava.dodoma.spring.aop;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class HelloTest {
聽protected static final Log log = LogFactory.getLog(HelloTest.class);
聽/**
聽 * @param args
聽 * @throws Exception
聽 */
聽public static void main(String[] args) throws Exception {
聽聽// TODO Auto-generated method stub
聽聽Resource rs = new ClassPathResource("beans.xml");
聽聽BeanFactory bf = new XmlBeanFactory(rs);
聽聽HelloI h = (HelloI) bf.getBean("theBean");
聽聽log.info("starting...");
聽聽try {
聽聽聽log.info(h.sayHello("ma", "bin"));
聽聽} catch (Exception e) {
聽聽聽e.printStackTrace();
聽聽}
聽聽log.info("end...");
聽聽
聽聽
聽聽ProxyFactory factory=new ProxyFactory();
聽聽factory.addAdvice(new LogThrowAdvice ());
聽聽factory.setTarget(new Hello("hello"));
聽聽try{
聽聽HelloI hi=(HelloI)factory.getProxy();
聽聽hi.sayHello("ma","bin");}
聽聽catch(Exception e){e.printStackTrace();}
聽}
}
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class LogAroundAdvice implements MethodInterceptor {
聽protected static final Log log = LogFactory.getLog(LogAroundAdvice.class);
聽public Object invoke(MethodInvocation arg) throws Throwable {
聽聽//聽璋冪敤鐩爣瀵硅薄涔嬪墠
聽聽log.info("before the target object");
聽聽Object val=arg.proceed();
聽 //璋冪敤鐩爣瀵硅薄涔嬪悗
聽聽log.info("the arg is "+arg);
聽聽log.info("after the target object");
聽聽return val;
聽}
}
嫻嬭瘯鏂規硶
package net.blogjava.dodoma.spring.aop;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class HelloTest {
聽protected static final Log log = LogFactory.getLog(HelloTest.class);
聽/**
聽 * @param args
聽 * @throws Exception
聽 */
聽public static void main(String[] args) throws Exception {
聽聽// TODO Auto-generated method stub
聽聽Resource rs = new ClassPathResource("beans.xml");
聽聽BeanFactory bf = new XmlBeanFactory(rs);
聽聽HelloI h = (HelloI) bf.getBean("theBean");
聽聽log.info("starting...");
聽聽try {
聽聽聽log.info(h.sayHello("ma", "bin"));
聽聽} catch (Exception e) {
聽聽聽e.printStackTrace();
聽聽}
聽聽log.info("end...");
聽聽
聽聽
聽聽ProxyFactory factory=new ProxyFactory();
聽聽factory.addAdvice(new LogAroundAdvice ());
聽聽factory.setTarget(new Hello("hello"));
聽聽try{
聽聽HelloI hi=(HelloI)factory.getProxy();
聽聽hi.sayHello("ma","bin");}
聽聽catch(Exception e){e.printStackTrace();}
聽}
}
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.AfterReturningAdvice;
public class LogAfterAdvice implements AfterReturningAdvice {
聽protected static final Log log = LogFactory.getLog(LogAfterAdvice.class);
聽public void afterReturning(Object returnVal, Method m, Object[] args,
聽聽聽Object target) throws Throwable {
聽聽// TODO Auto-generated method stub
聽聽log.info("in the class "+this.getClass().getName()+"'s method afterReturning()");
聽聽log.info("the target class is:" + target.getClass().getName());
聽聽log.info("the target method is:" + m.getName());
聽聽for (int i = 0; i < args.length; i++) {
聽聽聽log.info("the method's args is:" + args[i]);
聽聽}
聽聽
聽聽log.info("the returnValue is "+returnVal);
聽聽//嫻嬭瘯,濡傛灉榪斿洖瑁呭鍙戠敓浜嗗紓甯?灝嗗浣曞鐞嗙▼搴忔祦紼?br />聽聽//throw new Exception("榪斿洖瑁呭鍙戠敓寮傚父");
聽}
}
嫻嬭瘯浠g爜
package net.blogjava.dodoma.spring.aop;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class HelloTest {
聽protected static final Log log = LogFactory.getLog(HelloTest.class);
聽/**
聽 * @param args
聽 * @throws Exception
聽 */
聽public static void main(String[] args) throws Exception {
聽聽// TODO Auto-generated method stub
聽聽Resource rs = new ClassPathResource("beans.xml");
聽聽BeanFactory bf = new XmlBeanFactory(rs);
聽聽HelloI h = (HelloI) bf.getBean("theBean");
聽聽log.info("starting...");
聽聽try {
聽聽聽log.info(h.sayHello("ma", "bin"));
聽聽} catch (Exception e) {
聽聽聽e.printStackTrace();
聽聽}
聽聽log.info("end...");
聽聽
聽聽
聽聽ProxyFactory factory=new ProxyFactory();
聽聽factory.addAdvice(new LogAfterAdvice());
聽聽factory.setTarget(new Hello("hello"));
聽聽try{
聽聽HelloI hi=(HelloI)factory.getProxy();
聽聽hi.sayHello("ma","bin");}
聽聽catch(Exception e){e.printStackTrace();}
聽}
}
鍦╞eans.xml涓姞鍏ュ涓嬩唬鐮?br /><bean id="theLogAfterAdvice" class="net.blogjava.dodoma.spring.aop.LogAfterAdvice"/>
<property name="interceptorNames">
聽聽聽聽聽 <list>
聽聽聽聽聽聽聽 <value>theLogAfterAdvice</value><!--姝ゆ椂鐩存帴浣跨敤advice,琛ㄦ槑榪欎釜綾繪墍鏈夌殑瀹炰緥,鏂規硶,閮戒韓鍙梐dvice鐨勬嫤鎴?->
聽聽聽聽聽聽 </list>聽
</property>
鍒囧叆鐐瑰彲鐪佺暐,濡傞渶瑕佺殑璇?br />濡?br />
<!--鍒囧叆鐐?->
聽 <!--Note: An advisor assembles pointcut and advice-->
聽 <bean id="theBeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
聽聽聽 <property name="advice">
聽聽聽聽聽 <ref local="theLogAfterAdvice"/>
聽聽聽 </property>
聽聽聽 <!--鍖歸厤妯″紡-->
聽聽聽 <property name="pattern">
聽聽聽聽聽 <value>.*</value>
聽聽聽 </property>
聽 </bean>
public interface HelloI {
聽public String sayHello(String firstName,String lastName);
聽}
瀹炵幇綾?br />package net.blogjava.dodoma.spring.aop;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Hello implements HelloI {
聽protected static final Log log=LogFactory.getLog(Hello.class);
聽private String msg;
聽public Hello(){}
聽public Hello(String msg){
聽聽this.msg=msg;
聽}
聽public String getMsg() {
聽聽return msg;
聽}
聽public void setMsg(String msg) {
聽聽this.msg = msg;
聽}
聽public String sayHello(String firstName, String lastName) {
聽聽// TODO Auto-generated method stub
聽聽log.info("in the class "+this.getClass().getName()+"'s method sayHello()");
聽聽return (msg+" "+firstName+" "+lastName);
聽}
}
BeforeAdvice閫氱煡
package net.blogjava.dodoma.spring.aop;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.MethodBeforeAdvice;
/**
聽* 鏂規硶璋冪敤涔嬪墠.
聽* 鍏堣皟鐢ㄦ鏂規硶
聽* @author dodoma
聽**/
public class LogBeforeAdvice implements MethodBeforeAdvice {
聽protected static final Log log = LogFactory.getLog(LogBeforeAdvice.class);
聽public void before(Method m, Object[] args, Object target) throws Throwable {
聽聽log.info("in the class "+this.getClass().getName()+"'s method before()");
聽聽log.info("the target class is:" + target.getClass().getName());
聽聽log.info("the target method is:" + m.getName());
聽聽for (int i = 0; i < args.length; i++) {
聽聽聽log.info("the method's args is:" + args[i]);
聽聽}
聽聽//嫻嬭瘯,濡傛灉鍦╞efore閫氱煡涓彂鐢熶簡寮傚父,紼嬪簭嫻佺▼灝嗗浣?br />聽聽//throw new Exception("寮傚父");
聽}
}
嫻嬭瘯綾?br />package net.blogjava.dodoma.spring.aop;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class HelloTest {
聽protected static final Log log = LogFactory.getLog(HelloTest.class);
聽public static void main(String[] args) throws Exception {
聽聽// TODO Auto-generated method stub
聽//搴旂敤spring鐨刬oc瀹瑰櫒
聽聽Resource rs = new ClassPathResource("beans.xml");
聽聽BeanFactory bf = new XmlBeanFactory(rs);
聽聽HelloI h = (HelloI) bf.getBean("theBean");
聽聽log.info("starting...");
聽聽try {
聽聽聽log.info(h.sayHello("ma", "bin"));
聽聽聽聽聽} catch (Exception e) {
聽聽聽e.printStackTrace();
聽聽}
聽聽log.info("end...");
聽聽
聽聽//濡傛灉娌℃湁浣跨敤spring鐨刬oc,鍙互鐩存帴鐢ㄥ涓嬩唬鐮佹祴璇?br />聽聽ProxyFactory factory=new ProxyFactory();
聽聽factory.addAdvice(new LogBeforeAdvice());//娣誨姞閫氱煡
聽聽factory.setTarget(new Hello("hello"));//娣誨姞琚唬鐞嗙殑綾誨疄渚?br />聽聽try{
聽聽HelloI hi=(HelloI)factory.getProxy();
聽聽hi.sayHello("ma","bin");}
聽聽catch(Exception e){e.printStackTrace();}
聽}
}