使用AOP的好處,使用AOP好處

來源:互聯網
上載者:User

使用AOP的好處,使用AOP好處

原始代碼的寫法

既然要通過代碼來示範,那必須要有例子,這裡我的例子為:

有一個介面Dao有insert、delete、update三個方法,在insert與update被調用的前後,列印調用前的毫秒數與調用後的毫秒數

首先定義一個Dao介面:

public interface Dao {    public void insert();    public void delete();    public void update();}

然後定義一個實作類別DaoImpl:
public class DaoImpl implements Dao {    @Override    public void insert() {        System.out.println("DaoImpl.insert()");    }    @Override    public void delete() {        System.out.println("DaoImpl.delete()");    }    @Override    public void update() {        System.out.println("DaoImpl.update()");    }}

最原始的寫法,我要在調用insert()與update()方法前後分別列印時間,就只能定義一個新的類包一層,在調用insert()方法與update()方法前後分別處理一下
,新的類我命名為ServiceImpl,其實現為:
public class ServiceImpl {    private Dao dao = new DaoImpl();    public void insert() {        System.out.println("insert()方法開始時間:" + System.currentTimeMillis());        dao.insert();        System.out.println("insert()方法結束時間:" + System.currentTimeMillis());    }    public void delete() {        dao.delete();    }    public void update() {        System.out.println("update()方法開始時間:" + System.currentTimeMillis());        dao.update();        System.out.println("update()方法結束時間:" + System.currentTimeMillis());    }}

這是最原始的寫法,這種寫法的缺點也是一目瞭然:

方法調用前後輸出時間的邏輯無法複用,如果有別的地方要增加這段邏輯就得再寫一遍
如果Dao有其它實作類別,那麼必須新增一個類去封裝該實作類別,這將導致類數量不斷膨脹。

使用AOP

最後來看一下使用AOP的方式,首先定義一個時間處理類,我將它命名為TimeHandler:

public class TimeHandler {  public void printTime(ProceedingJoinPoint pjp) {        Signature signature = pjp.getSignature();        if (signature instanceof MethodSignature) {            MethodSignature methodSignature = (MethodSignature)signature;            Method method = methodSignature.getMethod();            System.out.println(method.getName() + "()方法開始時間:" + System.currentTimeMillis());      try {                pjp.proceed();                System.out.println(method.getName() + "()方法結束時間:" + System.currentTimeMillis());            } catch (Throwable e) {        e.printStackTrace();            }        }    }}
切面方法printTime本身可以不用定義任何的參數,但是有些情境下需要擷取調用方法的類、方法簽名等資訊,此時可以
在printTime方法中定義JointPoint,Spring會自動將參數注入,可以通過JoinPoint擷取調用方法的類、方法簽名等資訊
。由於這裡我用的aop:around,要保證方法的調用,這樣才能在方法調用前後輸出時間,因此不能直接使用JoinPoint,
因為JoinPoint沒法保證方法調用。此時可以使用ProceedingJoinPoint,ProceedingPointPoint的proceed()方法可以保
證方法調用,但是要注意一點,ProceedingJoinPoint只能和aop:around搭配,換句話說,如果aop.xml中配置的是aop:before,
然後printTime的方法參數又是ProceedingJoinPoint的話,Spring容器啟動將報錯。

接著看一下ApplicationContext.xml的配置:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  <bean id="daoImpl" class="org.xrq.spring.action.aop.DaoImpl" />
   <bean id="timeHandler" class="org.xrq.spring.action.aop.TimeHandler" />
  <aop:config>
    <aop:pointcut id="addAllMethod" expression="execution(* org.xrq.spring.action.aop.Dao.*(..))" />
    <aop:aspect id="time" ref="timeHandler">
    <aop:before method="printTime" pointcut-ref="addAllMethod" />
    <aop:after method="printTime" pointcut-ref="addAllMethod" />
    </aop:aspect>
  </aop:config>
</beans>

測試代碼很簡單:
public class AopTest {    @Test    @SuppressWarnings("resource")    public void testAop() {        ApplicationContext ac = new ClassPathXmlApplicationContext("spring/aop.xml");        Dao dao = (Dao)ac.getBean("daoImpl");        dao.insert();        System.out.println("----------分割線----------");        dao.delete();        System.out.println("----------分割線----------");        dao.update();    }}
到此我總結一下使用AOP的幾個優點:

  切面的內容可以複用,比如TimeHandler的printTime方法,任何地方需要列印方法執行前的時間與方法執行後的時間,都可以使用TimeHandler的printTime方法
避免使用Proxy、CGLIB組建代理程式,這方面的工作全部架構去實現,開發人員可以專註於切面內容本身
代碼與代碼之間沒有耦合,如果攔截的方法有變化修改設定檔即可。

 
 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.