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 閱讀(248) 評論(0)  編輯  收藏 所屬分類: spring
          主站蜘蛛池模板: 休宁县| 理塘县| 武隆县| 大英县| 赣州市| 阿鲁科尔沁旗| 农安县| 健康| 泰顺县| 宁化县| 奉新县| 海晏县| 宁德市| 金溪县| 盘山县| 井陉县| 仪征市| 东丽区| 汉沽区| 施甸县| 天柱县| 祁阳县| 海安县| 石河子市| 尼玛县| 射阳县| 台东市| 柞水县| 葫芦岛市| 名山县| 永济市| 宁德市| 和平县| 柯坪县| 仁怀市| 达孜县| 新竹市| 睢宁县| 灌南县| 肇东市| 沁水县|