當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
大数据WEB阶段Spring框架 AOP面向切面编程(一)
生活随笔
收集整理的這篇文章主要介紹了
大数据WEB阶段Spring框架 AOP面向切面编程(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Spring — AOP面向切面編程(一)
一、代理模式概述
二、靜態代理
1. 靜態代理優點
1. 解耦
2. 靜態代理缺點:
1. 一個代理類只能服務于一個被代理 , 如果被代理類很多 , 則操作會非常繁瑣
2. 代碼復用性不高 , 重復代碼太多
3. 代理類的通用性幾乎為0
4. 案例: 把service層的事務管理操作解耦
三、動態代理
1. 動態代理分為兩種:
1. JDK提供的動態代理
2. CGLib動態代理
2. JDK提供的動態代理
1. 優點:
1. 繼承靜態代理的優點 , 一個代理類可以為多個目標類服務
2. 提高代碼復用性
2. 缺點:
1. 并不是所有的方法都需要代理 , 需要進行方法過濾 , 過程繁瑣
3. 必須要求目標類有接口
4. 案例: 把service層的事務管理操作解耦
四、Spring AOP面向切面編程
AOP實現
編寫配置文件的頭
<?xml version="1.0" encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> </beans>編寫目標類
@Service public class PersonServiceImpl implements PersonService{@Autowiredprivate PersonDao dao;@Overridepublic void savePerson(Person person) {dao.savePerson(person);}}編寫切面和通知
@Component public class TxAspect {@Autowiredprivate TXManage tx;public Object around(ProceedingJoinPoint pjp) throws Throwable{tx.startTx();pjp.proceed();tx.commti();return null;} }配置切面表達式
<!-- 配置切面 --> <aop:config><!-- 配置切入點 一定要在切面的上面配置--><!-- within表達式只能過濾包和類 --><aop:pointcut expression="within(com.tj.service.*)" id="pc"/> </aop:config>配置切面和通知
<!-- 配置切面 --> <aop:config><!-- 配置切入點 一定要在切面的上面配置--><!-- within表達式只能過濾包和類 --><aop:pointcut expression="within(com.tj.service.*)" id="pc"/><!-- 配置切面 --><aop:aspect ref="txAspect" ><!-- 配置切面中的通知 --><aop:around method="around" pointcut-ref="pc"/></aop:aspect> </aop:config>總結
以上是生活随笔為你收集整理的大数据WEB阶段Spring框架 AOP面向切面编程(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大数据WEB阶段Spring框架 AOP
- 下一篇: 计算机视觉之OpenCV教程 --- M