日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AODp

發布時間:2023/12/13 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AODp 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、AOP是OOP的延續,是(Aspect Oriented Programming)的縮寫,意思是面向切面編程。

AOP(Aspect Orient Programming),作為面向對象編程的一種補充,廣泛應用于處理一些具有橫切性質的系統級服務,

如事務管理、安全檢查、緩存、對象池管理等。 AOP 實現的關鍵就在于 AOP 框架自動創建的 AOP 代理,

AOP 代理則可分為靜態代理和動態代理兩大類,其中靜態代理是指使用 AOP 框架提供的命令進行編譯,

從而在編譯階段就可生成 AOP 代理類,因此也稱為編譯時增強;

而動態代理則在運行時借助于 JDK 動態代理、CGLIB 等在內存中"臨時"生成 AOP 動態代理類,因此也被稱為運行時增強

AOP 的實現:

MyInterceptor、MyInterceptor2分別是以annotations和xml定義的切面類

?

package com.service;? ??

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

import org.aspectj.lang.annotation.Before;?

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

@Aspect?

public class MyInterceptor {?

??? @Pointcut("execution (* com.serviceImpl.PersonServiceImpl.*(..))")?

??? private void myMethod(){};? ?

??? @Before("myMethod()")?

??? public void doAccessCheck(){?

??????? System.out.println("before");?

??? }? ???

}

[java] view plain copy

package com.service;

public class MyInterceptor2 {?

??? public void doAccessCheck(){?

??????? System.out.println("before");?

??? }?

}?

業務和接口

[java] view plain copy

package com.service;? ??

public interface PersonService {?

??? public void save(String name);?

??? public void update(String name);?

}?

[java] view plain copy

package com.serviceImpl;?

import com.service.PersonService; ??

public class PersonServiceImpl implements PersonService {?

??? @Override?

??? public void save(String name) {?

??????? // TODO Auto-generated method stub?

??????? System.out.println("保存");?

??? }?

??? @Override?

??? public void update(String name) {?

??????? // TODO Auto-generated method stub?

??????? System.out.println("修改");?

??? }?

}

簡單做個方法前通知,其他的都一樣。

[java] view plain copy

<?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"?

??? xsi:schemaLocation="http://www.springframework.org/schema/beans?

?????????? http://www.springframework.org/schema/beans/spring-beans-2.5.xsd?

??????????? http://www.springframework.org/schema/aop?

?????????? http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">?

??? <aop:aspectj-autoproxy/>? ??

??? <bean id="personServiceImpl" class="com.serviceImpl.PersonServiceImpl"></bean>?

??? <bean id="personInterceptor" class="com.service.MyInterceptor2"></bean>? ?

??? <aop:config>?

??????? <aop:aspect id="asp" ref="personInterceptor">?

??????????? <aop:pointcut id="myCut" expression="execution (* com.serviceImpl.PersonServiceImpl.*(..))"/>?

??????????? <aop:before pointcut-ref="myCut" method="doAccessCheck"/>?

??????? </aop:aspect>??????

??? </aop:config>?

</beans> ?

測試類

[java] view plain copy

package com.test; ??

import org.junit.Test;?

import org.springframework.context.ApplicationContext;?

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

import com.service.PersonService;? ?

public class AopTest {? ???

??? @Test?

??? public void interceptorTest(){?

??????? ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");?

??????? PersonService ps = (PersonService) ac.getBean("personServiceImpl");?

??????? ps.save("aa");?

??? }?

} ?

轉載于:https://www.cnblogs.com/lihaiyang123/p/7084595.html

總結

以上是生活随笔為你收集整理的AODp的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。