<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    胖熊熊

    得到多少又失去了什么

    spring 學習(1.3) 入門基礎- AOP 實例

     aop 例子學習,轉(zhuǎn)自javaeye

    首先建一個普通Java項目:com.longthsoft.learn.spring

    spring.jar, commons-logging.jar, cglib-nodep-...jar, aspectjweaver.jar, aspectjrt.jar 放到 Build Path 下.

    以止 lib 除了 spring 外, 其他的都可以在 spring 下載包的 lib 中找到

    下面編碼開始:

    讓我們先寫兩個簡單的類:

    代碼

    1 package om.longthsoft.learn.spring.models;   
    2       
    3 public class A {   
    4     public void sayHello() {   
    5             System.out.println("Hello, I'm a");   
    6         }   
    7     }  
    8 

    1 package com.longthsoft.learn.spring.models;   
    2 public class B {   
    3         public void sayHi() {   
    4             System.out.println("Hi, I'm b");   
    5         }   
    6     }   
    7 


    沒什么實際的東西, 只是小A和小B在打招呼

    接下來把他們交給Spring吧(有點殘忍)。

    代碼

     1 <?xml version="1.0" encoding="UTF-8"?>  
     2 <beans xmlns="http://www.springframework.org/schema/beans"  
     3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     4         xmlns:aop="http://www.springframework.org/schema/aop"  
     5         xsi:schemaLocation="   
     6             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
     7             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
     8            
     9 <bean id="a" class="com.longthsoft.learn.spring.models.A" />  
    10 <bean id="b" class="com.longthsoft.learn.spring.models.B" />  
    11 </beans>  
    12 

    接下來寫個Boot

    代碼

     1     package com.longthsoft.learn.spring;   
     2       
     3     import org.springframework.context.ApplicationContext;   
     4     import org.springframework.context.support.ClassPathXmlApplicationContext;   
     5       
     6     import com.longthsoft.learn.spring.models.A;   
     7     import com.longthsoft.learn.spring.models.B;   
     8       
     9     public final class Boot {   
    10       
    11         public static void main(String[] args) {   
    12             ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");   
    13             A a = (A) ctx.getBean("a");   
    14             a.sayHello();   
    15                
    16             B b = (B) ctx.getBean("b");   
    17             b.sayHi();   
    18         }   
    19       
    20     }  
    21 

    嘿, 這里的運行結(jié)果不帖了, 大家腦子里閃過即可。

    圣誕到了, 小A小B 介紹完自己之后,也應該說句 "Merry Christmas"

    Spring 說, 既然你們交給我, 這等 routine 就不用再麻煩了, 直接一并處理掉。

    于是:

    代碼

     1     package com.longthsoft.learn.spring;   
     2       
     3     import org.aspectj.lang.annotation.AfterReturning;   
     4     import org.aspectj.lang.annotation.Aspect;   
     5     import org.aspectj.lang.annotation.Pointcut;   
     6       
     7     @Aspect  
     8     public class SimpleAspect {   
     9       
    10         @Pointcut("execution(* com.longthsoft.learn.spring.models.*.say*())")   
    11         public void simplePointcut() { }   
    12            
    13         @AfterReturning(pointcut="simplePointcut()")   
    14         public void simpleAdvice() {   
    15             System.out.println("Merry Christmas");   
    16         }   
    17     }   
    18 

    然后修改一下配置文件

    代碼

     1     <?xml version="1.0" encoding="UTF-8"?>  
     2     <beans xmlns="http://www.springframework.org/schema/beans"  
     3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     4         xmlns:aop="http://www.springframework.org/schema/aop"  
     5         xsi:schemaLocation="   
     6             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
     7             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
     8            
     9         <aop:aspectj-autoproxy />  
    10            
    11         <bean id="a" class="com.longthsoft.learn.spring.models.A" />  
    12         <bean id="b" class="com.longthsoft.learn.spring.models.B" />  
    13            
    14         <bean id="simpleAspect" class="com.longthsoft.learn.spring.SimpleAspect" />  
    15     </beans>  
    16 

    OK, 運行一下:

    Hello, I'm a
    Merry Christmas
    Hi, I'm b
    Merry Christmas

    posted on 2007-05-17 10:42 fatbear 閱讀(2321) 評論(1)  編輯  收藏 所屬分類: spring

    評論

    # re: spring 學習(1.3) 入門基礎- AOP 實例 2008-06-13 01:11 anna

    沒有提供applicationContext.xml的代碼  回復  更多評論   

    主站蜘蛛池模板: 午夜亚洲国产理论片二级港台二级 | 国产一级在线免费观看| 国产婷婷高清在线观看免费 | 亚洲精品偷拍无码不卡av| 久久九九AV免费精品| 久久精品国产亚洲沈樵| 一区二区三区四区免费视频| 色播亚洲视频在线观看| 999国内精品永久免费视频| 亚洲国产精品综合久久久| 无人影院手机版在线观看免费| 中文字幕 亚洲 有码 在线| 手机在线免费视频| 看一级毛片免费观看视频| 亚洲中文字幕无码专区| 成人免费无码H在线观看不卡| 国产亚洲一区二区三区在线不卡 | 6080午夜一级毛片免费看 | 国产成人综合亚洲一区| 亚洲日本韩国在线| 久久aa毛片免费播放嗯啊| 亚洲一区二区三区免费观看| 男女啪啪永久免费观看网站| 无套内谢孕妇毛片免费看看| 亚洲精品少妇30p| 中文毛片无遮挡高潮免费| 蜜臀亚洲AV无码精品国产午夜.| 亚洲精品国产福利一二区| 99久9在线|免费| 亚洲熟妇AV一区二区三区浪潮| mm1313亚洲精品国产| 久久精品成人免费看| 亚洲一区在线观看视频| 亚洲国产精品专区在线观看 | 亚洲综合一区二区精品导航| 四虎成人免费大片在线| 久久久久久久99精品免费观看| 亚洲国产精品免费观看| 黑人大战亚洲人精品一区| 免费三级毛片电影片| 一区二区在线免费视频|