Java學習路線-58:AOP面向切面編程

AOP 面向切面編程

AOP aspect oriented programming
OOP Object oriented programming

  1. 提供申明式服務
  2. 允許用戶實現自定義切面

傳統編程模式

自上而下,縱向的編程

Jsp
    ->
Action
    ->
Service
    ->
Dao

AOP 編程:
在不改變原有的代碼,增加新的功能

Jsp
    ->
Action
    ->
Service  <- log()
    ->
Dao

好處:

  1. 使得真實角色處理業務更加純粹,不再關注公共的問題
  2. 公共業務由代理類完成,實現業務的分工
  3. 公共業務發生擴展時變得更加集中和方便

關注點:日誌,安全,緩存,事務
切面 Aspect:一個關注點的模塊化

實現 AOP

1、通過 Spring 接口實現

依賴

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.5</version>
</dependency>

package com.spring.service;

public interface UserService {
    public void add();
    public void delete();
}

package com.spring.service.impl;

import com.spring.service.UserService;

public class UserServiceImpl implements UserService {

    @Override
    public void add() {
        System.out.println("add");
    }

    @Override
    public void delete() {
        System.out.println("delete");
    }
}

package com.spring.aop;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

// 前置通知
public class BeforeLog implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] args, Object target)
    throws Throwable {
        System.out.println(target.getClass().getName() + ": "+ method.getName());
    }
}

package com.spring.aop;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

// 後置通知
public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(
        Object result, Method method, Object[] objects, Object target)
        throws Throwable {
        System.out.println(target.getClass().getName() + ": "+ method.getName());
    }
}

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


    <bean id="service" class="com.spring.service.impl.UserServiceImpl"/>

    <bean id="beforeLog" class="com.spring.aop.BeforeLog"/>
    <bean id="AfterLog" class="com.spring.aop.AfterLog"/>

    <aop:config>
        <aop:pointcut id="action"
        expression="execution(* com.spring.service.impl.UserServiceImpl.* (..))"/>
        <aop:advisor advice-ref="beforeLog" pointcut-ref="action"/>
        <aop:advisor advice-ref="AfterLog" pointcut-ref="action"/>
    </aop:config>
</beans>

package com.spring.test;


import com.spring.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService service = (UserService) context.getBean("service");

        service.add();
        service.delete();

    }
}

執行結果

com.spring.service.impl.UserServiceImpl: add
add
com.spring.service.impl.UserServiceImpl: add

com.spring.service.impl.UserServiceImpl: delete
delete
com.spring.service.impl.UserServiceImpl: delete

Spring AOP 本質

就是講公共的業務(如日期,安全等)和領域業務結合,
當執行領域業務時將會把公共業務加進來,
實現公共業務的重複利用,
領域業務更純粹,可以專注於領域業務,
本質是動態代理

2、自定義類實現 AOP

UserService、UserServiceImpl、Demo 三個類不變

添加 Log 類和修改配置文件

package com.spring.aop;

public class Log {
    public void before(){
        System.out.println("--before--");
    }

    public void after(){
        System.out.println("--after--");
    }
}

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


    <bean id="service" class="com.spring.service.impl.UserServiceImpl"/>
    <bean id="log" class="com.spring.aop.Log"/>

    <aop:config>
        <aop:aspect ref="log">
            <aop:pointcut id="action"
            expression="execution(* com.spring.service.impl.UserServiceImpl.* (..))"/>
            <aop:before method="before" pointcut-ref="action"/>
            <aop:after method="after" pointcut-ref="action"/>
        </aop:aspect>
    </aop:config>
</beans>

執行結果

--before--
add
--after--
--before--
delete
--after--

3、註解實現 AOP
業務類不改變

package com.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class Log {
    @Before("execution(* com.spring.service.impl.UserServiceImpl.* (..))")
    public void before(){
        System.out.println("--before--");
    }

    @After("execution(* com.spring.service.impl.UserServiceImpl.* (..))")
    public void after(){
        System.out.println("--after--");
    }

    @Around("execution(* com.spring.service.impl.UserServiceImpl.* (..))")
    public Object Around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("--Around before--");
        Object result = pjp.proceed();
        System.out.println("--Around after--");
        return result;
    }
}

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


    <bean id="service" class="com.spring.service.impl.UserServiceImpl"/>
    <bean id="log" class="com.spring.aop.Log"/>

    <aop:aspectj-autoproxy />

</beans>

執行結果

--Around before--
--before--
add
--Around after--
--after--

--Around before--
--before--
delete
--Around after--
--after--

總結

公共業務:
日誌,安全,權限,緩存,事務

分離思想

在不改變原有代碼的情況下,增加額外的功能

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章