亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

一篇文章從無到有詳解Spring中的AOP

 更新時間:2021年08月25日 10:10:33   作者:笑容不是為了我  
。Spring AOP 是基于 AOP 編程模式的一個框架,它的使用有效減少了系統(tǒng)間的重復(fù)代碼,達到了模塊間的松耦合目的,這篇文章主要給大家介紹了關(guān)于Spring中AOP的相關(guān)資料,需要的朋友可以參考下

前言

AOP (Aspect Orient Programming),直譯過來就是 面向切面編程。AOP 是一種編程思想,是面向?qū)ο缶幊蹋∣OP)的一種補充。面向?qū)ο缶幊虒⒊绦虺橄蟪筛鱾€層次的對象,而面向切面編程是將程序抽象成各個切面。

從《Spring實戰(zhàn)(第4版)》圖書中扒了一張圖:

從該圖可以很形象地看出,所謂切面,相當于應(yīng)用對象間的橫切點,我們可以將其單獨抽象為單獨的模塊。

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置Service -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <!-- 注入dao -->
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!-- 通知方法 -->
    <!-- 配置事務(wù)管理器-->
    <bean id="txManager" class="com.itheima.utils.TransactionManager">
        <!-- 注入ConnectionUtils -->
        <property name="connectionUtils" ref="connectionUtils"></property>
    </bean>
    <!-- 切面 -->
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* com.itheima.service.impl.*.*(..))"/>

        <aop:aspect ref="txManager">
            <aop:before method="beginTransaction" pointcut-ref="pc"/>
            <aop:after-returning method="commit" pointcut-ref="pc"/>
            <aop:after-throwing method="rollback" pointcut-ref="pc"/>
            <aop:after method="release" pointcut-ref="pc"/>
        </aop:aspect>
    </aop:config>

AOP(Aspect Oriented Programming)

面向切面編程。橫向重復(fù),縱向抽取。
簡單的說它就是把我們程序重復(fù)的代碼抽取出來,在需要執(zhí)行的時候,使用動態(tài)代理的技術(shù),在不修改源碼的基礎(chǔ)上,對我們的已有方法進行增強。
實現(xiàn)原理:動態(tài)代理

事務(wù)處理方式

動態(tài)代理之前

在我們原始的控制事務(wù)中,
ConnectionUtils類:控制單線程內(nèi)只使用一個數(shù)據(jù)庫連接(connection)--->TransactionManager類 ,書寫方法:

  1) 打開手動提交事務(wù)conn.setAutoCommit(false);

  2) 提交事務(wù)conn.commit();

  3) 回滾事務(wù)conn.rollback();

  4) 釋放當前數(shù)據(jù)庫連接(手寫)。--->在業(yè)務(wù)層中,

try{
	1)       
	業(yè)務(wù)方法
	2)
}
catch(Throws t){  
 3) 
} finally{
 4)
}

在每個需要事務(wù)控制的方法都像這樣加上事務(wù)控制。

這樣書寫的業(yè)務(wù)層的代碼,過于臃腫,重復(fù)代碼過多。

動態(tài)代理實現(xiàn)

事先寫一個生成創(chuàng)建Service的代理對象的工廠類

/**
 * 用于創(chuàng)建Service的代理對象的工廠
 */
public class BeanFactory {

    private IAccountService accountService;

    private TransactionManager txManager;

    public void setTxManager(TransactionManager txManager) {
        this.txManager = txManager;
    }

    public final void setAccountService(IAccountService accountService) {
        this.accountService = accountService;
    }

    /**
     * 獲取Service代理對象
     * @return
     */
    public IAccountService getAccountService() {
        return (IAccountService)Proxy.newProxyInstance(accountService.getClass().getClassLoader(),
                accountService.getClass().getInterfaces(),
                new InvocationHandler() {
                    /**
                     * 添加事務(wù)的支持
                     *
                     * @param proxy
                     * @param method
                     * @param args
                     * @return
                     * @throws Throwable
                     */
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                        Object rtValue = null;
                        try {
                            //1.開啟事務(wù)
                            txManager.beginTransaction();
                            //2.執(zhí)行操作
                            rtValue = method.invoke(accountService, args);
                            //3.提交事務(wù)
                            txManager.commit();
                            //4.返回結(jié)果
                            return rtValue;
                        } catch (Exception e) {
                            //5.回滾操作
                            txManager.rollback();
                            throw new RuntimeException(e);
                        } finally {
                            //6.釋放連接
                            txManager.release();
                        }
                    }
                });

    }

BeanFactory 使用動態(tài)代理返回一個 IAccountService 對象并調(diào)用相應(yīng)方法。

把這個創(chuàng)建的對象存入Spring容器中,并注入原來的 accountService 和 txManager(事務(wù)管理工具類);

<!--配置beanfactory-->
<bean id="beanFactory" class="com.itheima.factory.BeanFactory">
    <!-- 注入service -->
    <property name="accountService" ref="accountService"></property>
    <!-- 注入事務(wù)管理器 -->
    <property name="txManager" ref="txManager"></property>
</bean>
把動態(tài)代理創(chuàng)建的 IAccountService ,也存入到 Spring 容器中。

<!--配置代理的service-->
<bean id="proxyAccountService" factory-bean="beanFactory" factory-method="getAccountService"></bean>

把動態(tài)代理創(chuàng)建的 IAccountService ,也存入到 Spring 容器中。

<!--配置代理的service-->
<bean id="proxyAccountService" factory-bean="beanFactory" factory-method="getAccountService"></bean>

測試方法:

/**
     * 使用Junit單元測試:測試我們的配置
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:bean.xml")
    public class AccountServiceTest {

        @Autowired
        @Qualifier("proxyAccountService")
        private  IAccountService as;

        @Test
        public  void testTransfer(){
            as.transfer("aaa","bbb",100f);
        }
}

在做業(yè)務(wù)層的事務(wù)控制時,可直接調(diào)用工廠類創(chuàng)建出的代理對象,實現(xiàn)事務(wù)控制,從而也使程序員在寫業(yè)務(wù)層時只管寫業(yè)務(wù),而不用管事務(wù)代碼。

當然我們能想到這樣的辦法,spring也早就封裝好了,在書寫xml配置文件時更加簡潔,可觀。

AOP的使用

  通過配置的方式實現(xiàn)上述功能,不用再自己書寫工廠類。

AOP的相關(guān)術(shù)語

  • Joinpoint(連接點):
    所謂連接點是指那些被攔截到的點。在spring中,這些點指的是方法,因為spring只支持方法類型的連接點。
  • Pointcut(切入點):
    所謂切入點是指我們要對哪些Joinpoint進行攔截的定義
  • Advice(通知/增強):
    所謂通知是指攔截到Joinpoint之后所要做的事情就是通知。
    通知的類型:前置通知,后置通知,異常通知,最終通知,環(huán)繞通知。
  • Introduction(引介):
    引介是一種特殊的通知在不修改類代碼的前提下, Introduction可以在運行期為類動態(tài)地添加一些方法或Field。
  • Target(目標對象):
    代理的目標對象。
  • Weaving(織入):
    是指把增強應(yīng)用到目標對象來創(chuàng)建新的代理對象的過程。
    spring采用動態(tài)代理織入,而AspectJ采用編譯期織入和類裝載期織入。
  • Proxy(代理):
    一個類被AOP織入增強后,就產(chǎn)生一個結(jié)果代理類。
  • Aspect(切面):
    是切入點和通知(引介)的結(jié)合。

學(xué)習(xí)Spring中的AOP要明確的事

a、開發(fā)階段(我們做的)

編寫核心業(yè)務(wù)代碼(開發(fā)主線):大部分程序員來做,要求熟悉業(yè)務(wù)需求。

把公用代碼抽取出來,制作成通知。(開發(fā)階段最后再做):AOP編程人員來做。

在配置文件中,聲明切入點與通知間的關(guān)系,即切面。:AOP編程人員來做。

b、運行階段(Spring框架完成的)

Spring框架監(jiān)控切入點方法的執(zhí)行。一旦監(jiān)控到切入點方法被運行,使用代理機制,動態(tài)創(chuàng)建目標對象的代理對象,根據(jù)通知類別,在代理對象的對應(yīng)位置,將通知對應(yīng)的功能織入,完成完整的代碼邏輯運行。

步驟

1、導(dǎo)包

2、書寫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"
       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">

    <!-- 配置Service -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <!-- 注入dao -->
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!-- 通知方法 -->
    <!-- 配置事務(wù)管理器-->
    <bean id="txManager" class="com.itheima.utils.TransactionManager">
        <!-- 注入ConnectionUtils -->
        <property name="connectionUtils" ref="connectionUtils"></property>
    </bean>
    <!-- 切面 -->
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* com.itheima.service.impl.*.*(..))"/>

        <aop:aspect ref="txManager">
            <aop:before method="beginTransaction" pointcut-ref="pc"/>
            <aop:after-returning method="commit" pointcut-ref="pc"/>
            <aop:after-throwing method="rollback" pointcut-ref="pc"/>
            <aop:after method="release" pointcut-ref="pc"/>
        </aop:aspect>
    </aop:config>

3.業(yè)務(wù)層代碼

public void transfer(String sourceName, String targetName, Float money) {
        System.out.println("transfer....");
            //2.1根據(jù)名稱查詢轉(zhuǎn)出賬戶
            Account source = accountDao.findAccountByName(sourceName);
            //2.2根據(jù)名稱查詢轉(zhuǎn)入賬戶
            Account target = accountDao.findAccountByName(targetName);
            //2.3轉(zhuǎn)出賬戶減錢
            source.setMoney(source.getMoney()-money);
            //2.4轉(zhuǎn)入賬戶加錢
            target.setMoney(target.getMoney()+money);
            //2.5更新轉(zhuǎn)出賬戶
            accountDao.updateAccount(source);

//            int i=1/0;

            //2.6更新轉(zhuǎn)入賬戶
            accountDao.updateAccount(target);
    }

4.測試

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {

    @Autowired
    @Qualifier("accountService")
    private  IAccountService as;

    @Test
    public  void testTransfer(){
        as.transfer("aaa","bbb",100f);
    }
}

使用spring的AOP基于動態(tài)代理開發(fā),簡潔的實現(xiàn)了該對象方法的增強,也就是實現(xiàn)了對轉(zhuǎn)賬的事務(wù)控制。

/**
* 使用Junit單元測試:測試我們的配置
*/
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:bean.xml")
    public class AccountServiceTest {
        @Autowired
        @Qualifier("proxyAccountService")
        private  IAccountService as;

        @Test
        public  void testTransfer(){
            as.transfer("aaa","bbb",100f);
        }
}

總結(jié)

到此這篇關(guān)于詳解Spring中AOP的文章就介紹到這了,更多相關(guān)詳解Spring中AOP內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Java的MyBatis框架與Spring框架整合中的映射器注入

    詳解Java的MyBatis框架與Spring框架整合中的映射器注入

    映射器注入方式可以將MyBatis與Spring映射好的XML文件實現(xiàn)配置共用,這里我們就來詳解Java的MyBatis框架與Spring框架整合中的映射器注入:
    2016-06-06
  • idea配置tomcat,idea配置web下lib的包詳解

    idea配置tomcat,idea配置web下lib的包詳解

    這篇文章主要介紹了idea配置tomcat,idea配置web下lib的包,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 如何解決java:錯誤:無效的源發(fā)行版:16

    如何解決java:錯誤:無效的源發(fā)行版:16

    這篇文章主要介紹了如何解決java:錯誤: 無效的源發(fā)行版:16問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Java開發(fā)之ssm三大框架整合

    Java開發(fā)之ssm三大框架整合

    SSM框架是spring?MVC?,spring和mybatis框架的整合,是標準的MVC模式,將整個系統(tǒng)劃分為表現(xiàn)層,controller層,service層,DAO層四層,使用spring?MVC負責請求的轉(zhuǎn)發(fā)和視圖管理,spring實現(xiàn)業(yè)務(wù)對象管理,mybatis作為數(shù)據(jù)對象的持久化引擎
    2022-05-05
  • java從控制臺接收一個數(shù)字的實例詳解

    java從控制臺接收一個數(shù)字的實例詳解

    這篇文章主要介紹了java從控制臺接收一個數(shù)字的實例詳解的相關(guān)資料,這里提供實例代碼,注釋說明清晰,需要的朋友可以參考下
    2017-07-07
  • 解決java.util.NoSuchElementException異常正確方法

    解決java.util.NoSuchElementException異常正確方法

    java.util.NoSuchElementException是Java中的一種異常,表示在迭代器或枚舉中找不到元素,這篇文章主要給大家介紹了關(guān)于解決java.util.NoSuchElementException異常的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • Springboot輕量級的監(jiān)控組件SpringbootAdmin

    Springboot輕量級的監(jiān)控組件SpringbootAdmin

    這篇文章主要為大家介紹了Springboot輕量級的監(jiān)控組件SpringbootAdmin使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Spring中常見的7種BeanDefinition詳解

    Spring中常見的7種BeanDefinition詳解

    在?Spring?容器中,我們廣泛使用的是一個一個的?Bean,BeanDefinition?從名字上就可以看出是關(guān)于?Bean?的定義,下面就跟隨小編一起深入了解一下常見的7中BeanDefinition吧
    2023-09-09
  • 利用Java快速查找21位花朵數(shù)示例代碼

    利用Java快速查找21位花朵數(shù)示例代碼

    這篇文章主要給大家介紹了關(guān)于利用Java快速查找21位花朵數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • Spring?Boot?使用觀察者模式實現(xiàn)實時庫存管理的步驟

    Spring?Boot?使用觀察者模式實現(xiàn)實時庫存管理的步驟

    在現(xiàn)代軟件開發(fā)中,實時數(shù)據(jù)處理非常關(guān)鍵,本文提供了一個使用SpringBoot和觀察者模式開發(fā)實時庫存管理系統(tǒng)的詳細教程,步驟包括創(chuàng)建項目、定義實體類、實現(xiàn)觀察者模式、集成Spring框架、創(chuàng)建RESTful?API端點和測試應(yīng)用等,這將有助于開發(fā)者構(gòu)建能夠即時響應(yīng)庫存變化的系統(tǒng)
    2024-09-09

最新評論