laoding
          本來我以為,隱身了別人就找不到我,沒有用的,像我這樣拉風的男人,無論走到哪里,都像在黑暗中的螢火蟲一樣,那樣的鮮明,那樣的出眾。我那憂郁的眼神,稀疏的胡茬,那微微隆起的將軍肚和親切的笑容......都深深吸引了眾人......
          posts - 0,  comments - 37,  trackbacks - 0
          上一篇中用XMLSchema配置方式介紹了簡單日志實現,這里再用annotation來介紹(注意是spring2.0)

          來看接口,這個接口簡單的不能再簡單了,嘻嘻。
          public interface Hello {
              String hello(String name);
          }

          實現類:

          public class SayHello implements Hello {

              
          public String hello(String name) {
                  String result 
          = "---hello " + name;
                  System.out.println(result);
                  
          return result;
              }

          }

          切面,里面采用了annotation來注釋,也說明了大概意思:

          /* 
           * Create Date:2008-11-20 下午03:09:11
           * 
           * Author:dingkm
           * 
           * Version: V1.0
           * 
           * Description:對進行修改的功能進行描述 
           * 
           *  
           
          */
          //首先這是注釋這個類就是切面
          @Aspect
          public class MyAspect {
              
          //這里是注釋要切入的方法,AfterReturning是表示方法返回以后進行切入,我這里
              
          //選這個的話是因為日志一般都是在方法執行完成后記錄,當然你可以拿Before來試
              @AfterReturning("execution(* *.aspectJ.*.hello(..))")
              
          public void doLog(ProceedingJoinPoint joinpoint) throws Throwable{
                  String result 
          = (String)joinpoint.proceed();
                  System.out.println(
          "---doLog"+result);
              }
          }

          下面是spring配置文件,這里的配置文件就比較簡單了:

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:aop
          ="http://www.springframework.org/schema/aop"
              xmlns:tx
          ="http://www.springframework.org/schema/tx"
              xmlns:jee
          ="http://www.springframework.org/schema/jee"
              xsi:schemaLocation
          ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
              http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"
          >
              
              
          <!--基于@AspectJ切面的驅動器,如果沒有這句話  切面的代碼是不會執行的,可以試下--> 
              
          <aop:aspectj-autoproxy/>
              
              
          <!--這個bean是作為切面    -->
              
          <bean id="myAspect" class="spring2aop.aspectJ.MyAspect"></bean>
              
              
          <!--要織入代碼的bean-->
              
          <bean id="hello" class="spring2aop.aspectJ.SayHello"></bean>

          </beans>

          <aop:aspectj-autoproxy/>這句很關鍵哦

          再來看測試類:

          public class Test {

              
          /**  
               *   @Description 方法實現功能描述  
               *   
          @param args
               *   void
               *   
          @throws  拋出異常說明
               
          */
              
          public static void main(String[] args) {
                  ApplicationContext act 
          = new ClassPathXmlApplicationContext(
                  
          "applicationContext21.xml");
                  Hello h 
          = (Hello)act.getBean("hello");
                  h.hello(
          "laoding");
              }

          }

          看看結果:

          ---hello laoding
          ---hello laoding
          ---doLog---hello laoding

          ---hello laoding這個與上一篇文章中提到的道理一樣,因為記錄日志要取得返回結果,所以執行了兩次

          最后的那句就是我們要的,這樣就達到了記錄日志的目的,哈哈,收工回去看PPS中韓魔獸對抗。
          posted on 2008-11-25 18:27 老丁 閱讀(1732) 評論(0)  編輯  收藏 所屬分類: spring
          本博客主為學習和復習之用,無關其他,想罵人的繞道
          Email:dkm123456@126.com
          大家一起交流進步
          QQ:283582761


          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          留言簿(4)

          我參與的團隊

          文章分類(50)

          文章檔案(48)

          相冊

          朋友

          搜索

          •  

          積分與排名

          • 積分 - 96533
          • 排名 - 600

          最新評論

          主站蜘蛛池模板: 蒙山县| 永登县| 普宁市| 柳河县| 霍山县| 安国市| 广丰县| 平泉县| 阿克| 娄烦县| 肥西县| 松潘县| 黄梅县| 建平县| 文安县| 定安县| 卢龙县| 南雄市| 安徽省| 弥渡县| 辽中县| 从化市| 麦盖提县| 分宜县| 永和县| 七台河市| 甘孜| 天津市| 尼勒克县| 崇文区| 乌什县| 新河县| 津南区| 临西县| 石嘴山市| 敦化市| 五河县| 和静县| 红原县| 启东市| 吉木萨尔县|