posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          spring AspectJ 基本使用

          Posted on 2008-05-08 10:08 G_G 閱讀(4330) 評論(1)  編輯  收藏 所屬分類: SpringAOP
          我的aop 基礎(chǔ)
          spring 實際使用 (這就使用一個例子說明)

          測試類以及結(jié)果:
          package?unit;

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

          import?bean.HelloService;

          public?class?Main?{
          ????
          public?static?void?main(String[]?args)?{
          ???????????ApplicationContext?context?
          =?new?ClassPathXmlApplicationContext(??????
          ???????????????????
          "beans.xml");????
          ???????????HelloService?service?
          =?(HelloService)?context.getBean("helloService");????
          ???????????service.annotationAop();
          ???????????
          ???????????System.out.println();
          ???????????
          ???????????service.xmlAop();
          ???????????
          ????}
          }
          結(jié)果:
          ?annotationAop//正常方法運行
          aop--AspectJ!?//
          annotation攔截

          ?xmlAop?
          //正常方法運行
          ?aop--XmlAop! //配置攔截



          use jar
          • --aspectjrt.jar
          • --aspectjweaver.jar
          • --acglib-nodep-2.1_3.jar
          • --commons-logging.jar
          • --spring.jar
          • --spring-aspects.jar
          XML配置
          <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"
          ???????????????xsi:schemaLocation
          =????????
          ???????????????????"http://www.springframework.org/schema/beans?????????
          ???????????????????http://www.springframework.org/schema/beans/spring-beans.xsd?????????
          ???????????????????http://www.springframework.org/schema/aop?????????
          ???????????????????http://www.springframework.org/schema/aop/spring-aop.xsd"
          >
          ????????
          ????????
          <!--?測試使用類?分別由?方法?annotationAop/xmlAop?-->
          ????????
          <bean?id="helloService"???????
          ????????????class
          ="bean.HelloService"/>????
          ????????
          ????????????
          ????????
          <!--?annotation?aop?攔截?使用@Aspect?
          ????????????@Pointcut("execution(*?annotationAop(..))")??
          ????????????@AfterReturning("mainMethod()")??
          ????????
          -->
          ????????
          <bean?id="xmlAop"?
          ????????????class
          ="aop.AnnotationAspectJ"/>
          ????????
          <aop:aspectj-autoproxy/>????
          ????????
          ????????
          ????????
          <!--?xml?aop??配置文件攔截?-->
          ????????
          <bean?id="XmlAspectJ"???????
          ????????????class
          ="aop.XmlAspectJ"/>
          ????????
          <aop:config>?????
          ?????????????
          <aop:aspect?ref="XmlAspectJ">
          ?????????????????
          <aop:pointcut?id="mainMethod"?expression="execution(*?xmlAop(..))"/>????
          ?????????????????
          <aop:after-returning?pointcut-ref="mainMethod"?method="goXmlAop"/>?????
          ?????????????
          </aop:aspect>???
          ?????????
          </aop:config>????
          ????????????
          </beans>

          HelloService.java
          package?bean;

          public?class?HelloService?{??
          ????
          public?void?annotationAop()?{????
          ????????System.out.println(
          "?annotationAop?");
          ????}
          ????
          ????
          public?void?xmlAop(){
          ????????System.out.println(
          "?xmlAop?");
          ????}
          }

          AnnotationAspectJ.java
          package?aop;

          import?org.aspectj.lang.annotation.AfterReturning;
          import?org.aspectj.lang.annotation.Aspect;
          import?org.aspectj.lang.annotation.Pointcut;

          @Aspect
          public?class?AnnotationAspectJ?{??
          ????@Pointcut(
          "execution(*?annotationAop(..))")??
          ????
          public?void?mainMethod()?{}??
          ????
          ????@AfterReturning(
          "mainMethod()")??
          ????
          public?void?sayHello()?{????
          ????????System.out.println(
          "aop--AspectJ!");??
          ????}
          }

          XmlAspectJ.java
          package?aop;

          public?class?XmlAspectJ?{
          ????
          public?void?goXmlAop(){
          ????????System.out.println(
          "?aop--XmlAop!?");
          ????}
          }














          評論

          # re: spring AspectJ 基本使用  回復(fù)  更多評論   

          2009-12-13 14:12 by 什么玩意亂糟糟的
          你寫的這是什么啊
          主站蜘蛛池模板: 尼木县| 辛集市| 龙口市| 明星| 盐边县| 舒兰市| 扎囊县| 乐安县| 新宁县| 尉氏县| 疏勒县| 南木林县| 宕昌县| 同德县| 和田市| 观塘区| 凤山县| 四子王旗| 新沂市| 资讯 | 安塞县| 安泽县| 郧西县| 乐亭县| 莱阳市| 元谋县| 萝北县| 峡江县| 东方市| 泾阳县| 涞源县| 株洲市| 汶川县| 张掖市| 阜新市| 故城县| 饶阳县| 贡嘎县| 宁乡县| 奉新县| 永安市|