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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring中AOP开发步骤

發(fā)布時(shí)間:2025/3/15 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring中AOP开发步骤 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
AOP:不是由Spring定義.AOP聯(lián)盟的組織定義.Spring中的通知:(增強(qiáng)代碼)前置通知 org.springframework.aop.MethodBeforeAdvice* 在目標(biāo)方法執(zhí)行前實(shí)施增強(qiáng)后置通知 org.springframework.aop.AfterReturningAdvice* 在目標(biāo)方法執(zhí)行后實(shí)施增強(qiáng)環(huán)繞通知 org.aopalliance.intercept.MethodInterceptor* 在目標(biāo)方法執(zhí)行前后實(shí)施增強(qiáng)異常拋出通知 org.springframework.aop.ThrowsAdvice* 在方法拋出異常后實(shí)施增強(qiáng)

第一步:導(dǎo)入相應(yīng)jar包.

* spring-aop-3.2.0.RELEASE.jar * com.springsource.org.aopalliance-1.0.0.jar

第二步:編寫被代理對(duì)象:

* CustomerDao接口 * CustoemrDaoImpl實(shí)現(xiàn)類

第三步:寫一個(gè)類繼承相關(guān)Advice的接口:

public class MyBeforeAdvice implements MethodBeforeAdvice{/*** method:執(zhí)行的方法* args:參數(shù)* target:目標(biāo)對(duì)象*/public void before(Method method, Object[] args, Object target)throws Throwable {System.out.println("前置增強(qiáng)...");} }

?

第四步:生成代理:(配置生成代理:)

spring是支持配置文件幫我們生成代理的。<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 目標(biāo)對(duì)象 --><bean id="customerDao" class="cn.itcast.aop.dao.CustomerDaoImpl"/><!-- 定義增強(qiáng)對(duì)象 --> <bean id="beforeAdvice" class="cn.itcast.aop.advice.MyBeforeAdvice"/> <!-- 使用spring框架生成代理對(duì)象 --> <bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 代理的目標(biāo)對(duì)象 --> <property name="target" ref="customerDao"></property> <!-- 設(shè)置實(shí)現(xiàn)的接口告訴代理實(shí)現(xiàn)的接口跟CustomerDaoImpl實(shí)現(xiàn)類實(shí)現(xiàn)的接口一樣 --> <property name="proxyInterfaces" value="cn.itcast.aop.dao.CustomerDao"></property> <!-- 增強(qiáng)是通知name="interceptorNames"攔截所有的方法 ? ?value="beforeAdvice" 增強(qiáng)的通知告訴代理對(duì)象應(yīng)該使用前置增強(qiáng)--> <property name="interceptorNames" value="beforeAdvice"></property> </bean></beans>

第五步:編寫測(cè)試類

利用spring集成junit將我們需要的目標(biāo)類注入到測(cè)試類中

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class SpringTest {@Autowired// @Qualifier("customerDao")//注入真實(shí)對(duì)象@Qualifier("customerDaoProxy")//注入代理對(duì)象private CustomerDao customerDao;@Testpublic void test(){customerDao.add();customerDao.delete();customerDao.find();customerDao.update();} }

?

總結(jié)

以上是生活随笔為你收集整理的Spring中AOP开发步骤的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。