亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Spring AOP面向切面編程實(shí)現(xiàn)及配置詳解

瀏覽:2日期:2023-08-15 08:23:25

動(dòng)態(tài)代理

特點(diǎn)

字節(jié)碼隨用隨創(chuàng)建,隨用隨加載

作用

不用修改源碼對(duì)方法增強(qiáng)

分類

基于接口的動(dòng)態(tài)代理

基于子類的動(dòng)態(tài)代理

創(chuàng)建

使用Proxy類中的newProxyInstance方法

要求

被代理類最少實(shí)現(xiàn)一個(gè)接口,沒有則不能使用

newProxyInstance方法參數(shù)

classLoader:類加載器

用于加載代理對(duì)象字節(jié)碼的,和被代理對(duì)象使用相同的類加載器

class[ ]:字節(jié)碼數(shù)組

用于讓代理對(duì)象和被代理對(duì)象有相同方法,固定寫法。

InvocationHandler:用于提供增強(qiáng)的代碼

是讓我們寫如何代理。一般都是寫一個(gè)該接口的實(shí)現(xiàn)類,通常情況下都是匿名內(nèi)部類,不是必須的

此接口的實(shí)現(xiàn)類都是誰用誰寫

IProducer proxyProducer = (IProducer) Proxy.newProxyInstance(producer.getClass().getClassLoader(),producer.getClass().getInterfaces(),new InvocationHandler(){ 作用:執(zhí)行被代理對(duì)象的任何接口方法都會(huì)經(jīng)過該方法 * proxy 代理對(duì)象的引用 * method 當(dāng)前執(zhí)行的方法 * args 執(zhí)行當(dāng)前方法所需的參數(shù) * return 和被代理對(duì)象有相同的返回值@overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable{// 提供增強(qiáng)的代碼Object returnValue = null1. 獲取方法執(zhí)行的參數(shù)Float money = (Float)args[0]2. 判斷當(dāng)前方法是否為指定方法if('saleProduct'.equals(method.getName())){returnValue = method.invoke(producer,money*0.8)}return returnValue;}})//代理方法調(diào)用的是上面invoke中的方法proxyProducer.saleProduct(100000)

注意 如果代理的類沒有接口,則代理不可用。

AOPxml配置

連接點(diǎn)Joinpoint:指那些被攔截的點(diǎn),在spring中,這些點(diǎn)指的是方法,因?yàn)閟pring只支持方法類型的連接點(diǎn)。

切入點(diǎn)Pointcut:所謂切入點(diǎn)指的是要對(duì)哪些Joinpoint進(jìn)行攔截的定義。方法會(huì)被增強(qiáng)。

所有的切入點(diǎn)都是連接點(diǎn),但不是所有的連接點(diǎn)都是切入點(diǎn)。

通知Advice:指攔截到Joinpoint之后所要做的事情

在invoke方法里的,有前置通知,后置通知,異常通知,最終通知

引入Introduction

目標(biāo)對(duì)象Target :即被代理的對(duì)象

織入Weaving:把增強(qiáng)應(yīng)用到目標(biāo)對(duì)象來創(chuàng)建新的代理對(duì)象的過程。Spring采用動(dòng)態(tài)代理織入。

創(chuàng)建接口類,實(shí)現(xiàn)類

創(chuàng)建aop通知功能函數(shù)

xml配置

<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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd'> <!--配置spring的IOC,把service對(duì)象配置進(jìn)來--> <bean class='hjj.web.service.impl.AccountServiceImpl'></bean> <!--spring中基于xml的aop配置步驟 1. 把通知bean也交給spring來管理 2. 使用aop:config標(biāo)簽表明aop的配置 3. 使用aop:aspect標(biāo)簽表明配置切面id:給切面提供一個(gè)唯一表示ref:指定通知類bean的id 4. 在aop:aspect標(biāo)簽的內(nèi)部使用對(duì)應(yīng)的標(biāo)簽來配置通知的類型現(xiàn)在讓pringLog方法在切入點(diǎn)方法執(zhí)行前執(zhí)行aop:before表示配置前置通知 method:用于指定Logger類中哪個(gè)方法是前置通知 point屬性:用于指定切入點(diǎn)表達(dá)式,該表達(dá)式指的是對(duì)業(yè)務(wù)層中哪些方法增強(qiáng) 切入點(diǎn)表達(dá)式: 關(guān)鍵字:execution(表達(dá)式) 訪問修飾符 返回值 包名.類名.方法名(參數(shù)列表) 全通配寫法:* *..*.*(..) 訪問修飾符可以省略 *可以代表任何返回值 *.*.*可以表示包的關(guān)系 *..表示中間任意包 *.* 表示類名和方法 (..)表示任意參數(shù)或者可以寫返回值類型 int, java.lang.String 實(shí)際開發(fā)寫法:切到業(yè)務(wù)層實(shí)現(xiàn)類下的所有方法 * 業(yè)務(wù)層包.*.*(..) --> <!--配置logger類--> <bean class='hjj.web.utils.Logger'></bean> <!--配置AOP--> <aop:config> <!--配置切面--> <aop:aspect ref='logger'><!--配置通知類型,并且建立通知方法和切入點(diǎn)方法的關(guān)聯(lián)--><aop:before method='printLog' pointcut='execution(public void hjj.web.service.impl.AccountServiceImpl.saveAccount())'></aop:before> </aop:aspect> </aop:config>// 通知類型 <aop:aspect ref='logger'><!--配置通知類型,并且建立通知方法和切入點(diǎn)方法的關(guān)聯(lián)--><!--<aop:before method='printLog' pointcut='execution(public void hjj.web.service.impl.AccountServiceImpl.saveAccount())'></aop:before>--><aop:before method='beforePrintLog' pointcut='execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())'></aop:before><aop:after-returning method='afterPrintLog' pointcut='execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())'></aop:after-returning><aop:after-throwing method='afterThrowingPringLog' pointcut='execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())'></aop:after-throwing><aop:after method='finalPrintLog' pointcut='execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())'></aop:after> </aop:aspect> </beans>

<!-- 配置切入點(diǎn)表達(dá)式,ID屬性用于指定表達(dá)式的唯一標(biāo)識(shí),expression屬性用于指定表達(dá)式內(nèi)容,此標(biāo)簽也可以放在aspect外面--> <aop:pointcut expression='execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())'/> <aop:before method='beforePrintLog' pointcut-ref='pt1'></aop:before>

AOPxml注解

aop注解配置

/** * 記錄日志的工具類,提供了公共的代碼 */@Component('logger')@Aspect // 表示當(dāng)前類是一個(gè)切面public class Logger {@Pointcut('execution()')private void pt1(){} /** * 用于打印日志:計(jì)劃在其切入點(diǎn)方法執(zhí)行前執(zhí)行(切入點(diǎn)方法就是業(yè)務(wù)層方法) */ @Before(pt1()) public void beforePrintLog() { System.out.println('前置'); } public void afterPrintLog() { System.out.println('后置'); } public void afterThrowingPringLog() { System.out.println('異常'); } public void finalPrintLog() { System.out.println('最終'); } // 環(huán)繞通知為我們提供了ProceedingJoinPoint,有一個(gè)方法proceed(),此方法就明確了調(diào)用切入點(diǎn)方法 // 為我們提供了一種可以在代碼中手動(dòng)控制增強(qiáng)方法合適執(zhí)行的方式 public Object aroundPrintLog(ProceedingJoinPoint pjp) { Object returnValue = null; try { Object[] args = pjp.getArgs(); // 得到方法執(zhí)行所需參數(shù) System.out.println('前置'); returnValue = pjp.proceed(args); // 明確調(diào)用業(yè)務(wù)層的方法 System.out.println('后置'); } catch (Throwable throwable) {// throwable.printStackTrace(); System.out.println('異常'); } finally { System.out.println('最終'); } return returnValue;// System.out.println('環(huán)繞通知'); }}

xml:

配置spring創(chuàng)建容器要掃描的包

<context:component-scan base-package='包路徑'></context:component-scan><aop:aspectj-autoproxy></aop:aspectj-autoproxy>

注意 如果用注解自帶的調(diào)用順序會(huì)出現(xiàn)問題,用環(huán)繞通知順序正常

事務(wù)控制

導(dǎo)包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.2.4.RELEASE</version></dependency>

事務(wù)管理器:org.springframework.orm.hibernate5.hibernate5.HibernateTransactionManager

在bean.xml中配置

1. 配置事物管理器

<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' xmlns:tx='http://www.springframework.org/schema/tx' xsi:schemaLocation=' http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd'><bean class='org.springframework.orm.hibernate5.hibernate5.HibernateTransactionManager'><property name='dataSource' ref='dataSource'><bean>

2.配置事物的通知

<tx:advice transaction-manager='transactionManager'>

5.配置事物的屬性

<tx:attributes><tx:method name='*' propagation='required' read-only=’false’/><tx:method name='find*' propagation='support' read-only=’true’/>isolation:指定事物的隔離級(jí)別,默認(rèn)值是default,表示使用數(shù)據(jù)庫的默認(rèn)隔離級(jí)別propagation:用于指定事物的傳播行為,默認(rèn)是REQUIRED,表示一定會(huì)有事物,增刪改的選擇,查詢可以使用supportread-only:用于指定事物是否只讀,查詢才設(shè)置為truetimeout:用于指定事物的超市時(shí)間,默認(rèn)值是-1,表示不超時(shí),如果指定了數(shù)值,以秒為單位rollback-for:用于指定一個(gè)異常,當(dāng)產(chǎn)生該異常時(shí)事物回滾,產(chǎn)生其他異常時(shí),事物不回滾。沒有默認(rèn)值,表示任何異常都回滾no-rollback-for:用于指定一個(gè)異常,當(dāng)產(chǎn)生該異常,事務(wù)不會(huì)回滾,產(chǎn)生其他異常,事務(wù)回滾。沒有默認(rèn)值,表示任何異常都回滾。</tx:attributes></tx:advice>

3.配置aop切入點(diǎn)表達(dá)式

<aop:config><aop:pointcut expression='execute(* 包.包.*.*(..))'>

4. 建立切入點(diǎn)表達(dá)式喝事物通知的對(duì)應(yīng)關(guān)系

<aop:advisor advice-ref='txAdvice' pointcut-ref='pt1'></aop>

<beans>

基于注解的事務(wù)控制

1. 配置事物管理器

<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' xmlns:tx='http://www.springframework.org/schema/tx' xmlns:context='http://www.springframework.org/schema/context' xsi:schemaLocation=' http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd' http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd'>

3. 配置spring創(chuàng)建容器時(shí)要掃描的包

<context:component-scan base-package='包的地址'>

4. 開啟spring對(duì)注解事物的支持

<tx:annotation-driven transaction-manager='transactionManager>'

6. 在需要事物支持的地方使用注解@Transactional

2.在實(shí)現(xiàn)類中

@Service(accountService)@Transactionalpublic class 實(shí)現(xiàn)類 implements 接口類{@Autowired// 在持久層也要配置private IaccountDao accountDao}

基于注解的配置類

1.創(chuàng)建一個(gè)配置總配置類

@Configuration// 用于配置需要掃描的包@ComponentScan('hjj.web')@Import({HibernateConfig.class, TransactionConfig.class})@PropertySource('hibernateConfig.properties')@EnableTransactionManagement //開啟注解的支持public class SpringConfiguration{}

2.另一個(gè)java類,連接數(shù)據(jù)庫相關(guān)的類

publci class HibernateConfig{@Value('${hibernate.username}')private String username;@Value('${hibernate.password}')private String password// 注入進(jìn)容器@Bean(name='HibernateTemplate')public Hibernate crateHibernateTemplate(DataSource datasource){return new HibernateTemplate(dataSource)}@Bean(name='dataSource')public DataSource crateDataSource(){配置數(shù)據(jù)庫的用戶名密碼 創(chuàng)建數(shù)據(jù)源對(duì)象}}

3. 新建一個(gè)properties,配置文件類

hibernate.username = hibernate.password =

4. 創(chuàng)建和事物相關(guān)的配置類

public class TransactionConfig {//創(chuàng)建事務(wù)管理器對(duì)象@Bean(name='transactionManager')public PlatformTransactionManager createTransactionManager(DataSource dataSource){return new DataSourceTransactionManager(dataSource)}}

5. main方法所在的類

@ContextConfiguration(classes=SpringConfiguration.class)public class test{psvm{業(yè)務(wù)邏輯}}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 精品国产三级a∨在线观看 精品国产三级在线观看 | 久青草青综合在线视频 | 色播网址 | 中日一级片 | 国产日韩高清一区二区三区 | 国产无遮挡色视频免费视频 | 国产在线视频二区 | 国产日产欧美精品一区二区三区 | 网友自拍视频在线 | 国产玖玖视频 | 丁香六月激情婷婷 | 精品香蕉伊思人在线观看 | 国产精品久久久久久 | 亚洲精品国产一区二区 | 色综合99天天亚洲 | 香蕉99 | 国产成人午夜性视频影院 | 青草精品| 日韩在线视频中文字幕 | 色综合久久夜色精品国产 | 五月综合激情久久婷婷 | 午夜男人一级毛片免费 | 国自产精品手机在线视频香蕉 | 欧美成人国产一区二区 | 亚洲高清免费视频 | 6080欧美一区二区三区四区 | 91最新在线| 免费区欧美一级毛片精品 | 国产在线观看人成激情视频 | 成人亚洲综合 | 久久久成人啪啪免费网站 | 国产xxxx做受欧美88xx00tube | 国产亚洲精品国产一区 | 国产刚刚发育被强j在线播放 | 最新国产成人盗摄精品视频 | 观看麻豆影视文化有限公司 | 亚洲1卡2卡三卡3卡4卡网站 | 狠狠色婷婷综合天天久久丁香 | 日本高清免费中文字幕不卡 | 成人黄色在线免费观看 | 全黄一级裸片视频免费 |