posts - 10,comments - 4,trackbacks - 0

          接口修改為
          package net.blogjava.dodoma.spring.aop;

          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();}
          ?}

          }

          posted on 2006-03-28 12:57 dodoma 閱讀(247) 評論(0)  編輯  收藏 所屬分類: spring

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 象州县| 丰台区| 石嘴山市| 象山县| 凌海市| 客服| 黎川县| 苗栗市| 瑞昌市| 孟津县| 蕲春县| 上饶市| 彭泽县| 扶余县| 诏安县| 仙居县| 容城县| 香港| 东宁县| 探索| 贵港市| 东辽县| 乌什县| 大余县| 台北县| 南阳市| 手游| 江安县| 牡丹江市| 鄂温| 汝州市| 刚察县| 沈阳市| 新昌县| 平果县| 百色市| 大关县| 永靖县| 华坪县| 澄城县| 理塘县|