模仿spring-aop的功能,利用註解搭建自己的架構。,spring-aop註解

來源:互聯網
上載者:User

模仿spring-aop的功能,利用註解搭建自己的架構。,spring-aop註解

  入JAVA坑7月有餘,也嘗試自己手動搭建架構,最近對spring aop的這種切面很著迷,為此記錄下自己目前搭出來的小小的demo,後續有時間也會繼續改進自己的demo。望大神們不吝賜教。

  主要還是運用反射和java內建的代理類。理論知識就不說了,因為我目前也不是很清楚,避免誤導,還是避而不談吧。好了,直接根據代碼擼吧。

  結構:

  

  

介面 
Person.java
public interface Person {    void say();}

 

介面實作類別

Man.java
public class Man implements Person {    @Override    public void say() {        System.out.println("男人say:....");    }}

 

自訂註解

@interface WaterAOP
@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.PARAMETER, ElementType.METHOD})@Documentedpublic @interface WaterAOP {    enum METHOD{before,after,afterthrowing}    METHOD method() default METHOD.after;    String Name() default "類全名";}

自訂註解類

WaterLog.java
public class WaterLog {    @WaterAOP(Name = "com.water.aop.attempt3.Man",method = WaterAOP.METHOD.after)    public void afterAction(){        System.out.println("後置行為");    }    @WaterAOP(Name = "com.water.aop.attempt3.Man",method = WaterAOP.METHOD.before)    public void beforeAction(){        System.out.println("前置行為");    }}

實現自訂代理類(就是在

Proxy.newProxyInstance()方法的第三個參數裡做手腳。用了java8的lambda運算式。

)

  ProxyFactory.java

public class ProxyFactory {    // 維持一個實現介面的被代理的對象,後面改為對象組,由淺入深    private Person person;    private WaterLog waterLog;    private Method beforeMethod=null,afterMethod=null;    public ProxyFactory(Person person,WaterLog waterLog){        this.person=person;        this.waterLog=waterLog;    }    public Object getProxyInstance(){        return Proxy.newProxyInstance(                person.getClass().getClassLoader(),                person.getClass().getInterfaces(),//                第一個參數就是代理者,如果你想對代理者做一些操作可以使用這個參數;//                第二個就是被執行的方法,//                第三個是執行該方法所需的參數。                (Object proxyObj, Method method,Object[] args)->{                    //如果沒有傳入aop 直接返回空                    if(waterLog==null){                        return null;                    }                    Class aop=waterLog.getClass();                    Class c = person.getClass();                    // 擷取aop類的方法的註解並賦給自訂的一些變數,下面根據這些變數是否有值來確定是否有註解                    getAnnotation(aop,c);                    if(beforeMethod!=null){                        beforeMethod.invoke(waterLog);                    }                    // 代理對象執行方法並且獲得傳回值                    Object returnValue=method.invoke(person,args);                    if(afterMethod!=null){                        afterMethod.invoke(waterLog);                    }                    return returnValue;                }        );    }    private void getAnnotation(Class aop,Class proxy){        //如果有AOP的類        if(waterLog!=null){            // 擷取切面類所有的方法            Method[] methodsAOP=aop.getMethods();            // 如果切入的日誌類的方法不為空白            if(methodsAOP!=null){                for(Method logMethod:methodsAOP){                    // 取得WaterLog類的方法上WaterAOP註解                    WaterAOP waterAOP=logMethod.getAnnotation(WaterAOP.class);                    if(waterAOP!=null) {                        // 如果AOP上的註解與傳入的類名一致                        if (proxy.toString().substring(6).equals(waterAOP.Name())) {                            if (waterAOP.method() == WaterAOP.METHOD.before) {                                // 賦值 ,後面再執行                                beforeMethod=logMethod;                            }else if(waterAOP.method() == WaterAOP.METHOD.after){                                afterMethod=logMethod;                            }                        }                    }                }            }        }    }}

測試類別

Test.java (junit是個測試包,也可以直接用main方法)

public class Test {    @org.junit.Test    public void waterAOP(){        Person person=new Man();        Person proxyPerson=(Person) new ProxyFactory(person,new WaterLog()).getProxyInstance();        proxyPerson.say();    }}

 

大致的流程就是:傳入要被代理的類和自訂的註解類,運用反射擷取註解類裡方法上的註解屬性的值,然後進行比對,再進行相應的操作。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.