基于AspectJ和基于schema的Aop命名的使用
在main方法中寫(xiě)上 System.getProperties().put(“sun.misc.ProxyGenerator.saveGeneratedFiles”, “true”);可以在com.sun.proxy路徑下看到生成的代理類(lèi)class
基本概念目標(biāo)對(duì)象的接口以及實(shí)現(xiàn)
public interface Hello {void say();
    String tell(String mes);
}
public class HelloImpl implements Hello {@Override
    public void say() {System.out.println("hi");
    }
    @Override
    public String tell(String mes) {System.out.println(mes);
        return mes;
    }
}實(shí)現(xiàn)一個(gè)切面
@Aspect
public class BeformAspect {@Before("execution(* say(..))")
    public void print()
    {System.out.println("before");
    }
}
使用
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello)applicationContext.getBean("hello");
hello.say();
| 符號(hào) | 作用 | 
|---|---|
| * | 匹配任意字符,但只能匹配上下文種的一個(gè) | 
| …(兩個(gè)點(diǎn)) | 匹配任意字符可以匹配上下文多個(gè),但在表示類(lèi)的時(shí)候,必須和*聯(lián)合使用,表示入?yún)为?dú)使用 | 
| + | 必須跟在類(lèi)名后,代表類(lèi)的子類(lèi),包括自己 | 


| 注解 | 參數(shù) | 作用 | 
|---|---|---|
| @Before (前置) | value | 定義切點(diǎn) | 
| argNames | 可以獲得目標(biāo)對(duì)象的參數(shù),參數(shù)名要和目標(biāo)的參數(shù)名一致,多個(gè)已逗號(hào)隔開(kāi) | |
| @AfterReturning (后置) | value | 定義切點(diǎn) | 
| pointcut | 顯示定義將覆蓋value,和value同義 | |
| returning | 將目標(biāo)的返回值綁定給增強(qiáng)方法 | |
| argNames | 同前面一樣 | |
| @Around (環(huán)繞) | value | 定義切點(diǎn) | 
| argNames | 同前面一樣 | |
| @AfterThrowing (異常) | value | 定義切點(diǎn) | 
| pointcut | 同前面一樣 | |
| throwing | 將拋出的異常綁定到方法中 | |
| argNames | 同前面一樣 | |
| @After (不管異常還是正常退出都會(huì)執(zhí)行) | value | 定義切點(diǎn) | 
| argNames | 同前面一樣 | |
| @DeclareParents (引介增強(qiáng)) | value | 定義切點(diǎn) | 
| defaultImpl | 默認(rèn)的接口實(shí)現(xiàn)類(lèi) | 
比較特殊不同于其他增強(qiáng)類(lèi)型的使用,
將HelloImpl融入SellerImpl類(lèi)
public interface Hello {void say();
    String tell(String mes);
}
public class HelloImpl implements Hello {@Override
    public void say() {System.out.println("hi");
    }
    @Override
    public String tell(String mes) {System.out.println(mes);
        return mes;
    }
}
public interface Seller {String sell(String goods);
}
public class SellerImpl implements Seller{@Override
    public String sell(String goods) {System.out.println(goods);
        return null;
    }
}介引切面
@Aspect
public class BeformAspect {@DeclareParents(value = "AspectJ.HelloImpl",defaultImpl = SellerImpl.class)
    public Seller seller;
}配置文件
使用
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        //可以將HelloImpl強(qiáng)轉(zhuǎn)為Seller
        Seller hello = (Seller)applicationContext.getBean("hello");
        System.out.println(hello);@Before("@annotation(AspectJ.Mytest)")被注解標(biāo)記的方法會(huì)執(zhí)行切面注入
execution 通過(guò)方法簽名定義匹配 所有public的方法(其他修飾符不行),第一個(gè)*代表返回值 第二個(gè)代表方法名,(…)表示任意入?yún)?/p>
@Before("execution(public * *(..))")任意返回值,任意參數(shù),方法名字以ll結(jié)尾的方法
@Before("execution(* *ll(..))")匹配Hello接口所有實(shí)現(xiàn)類(lèi),第一個(gè)*為返回值。(只包括接口定義的方法)
@Before("execution(* AspectJ.Hello.*(..))")匹配Hello接口所有實(shí)現(xiàn)類(lèi),第一個(gè)*為返回值。(包括子類(lèi)里不是接口的方法)
@Before("execution(* AspectJ.Hello+.*(..))")你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
                本文標(biāo)題:SpringAop使用-創(chuàng)新互聯(lián)
                
                瀏覽路徑:http://www.chinadenli.net/article24/dheoje.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、品牌網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站收錄、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
