Spring之AOP之底層實現(xiàn)

Spring之AOP之底層實現(xiàn)

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

  • 定義IStudnetService接口
/**
 * IStudentService的接口
 * @author chenjiabing
 */
public interface IStudentService {

    void add();
}
  • 定義StudentServiceImpl實現(xiàn)方法
@Service
public class StudentServiceImpl implements IStudentService {

    public void add() {
        System.out.println("StudentService的add方法");
    }
}
  • 定義StudentAop切面類
/**
 * 切面類
 * @author chenjiabing
 */
@Component
public class StudentAOP {
    //之前執(zhí)行
    public void before(){
        System.out.println("StudentAOP.before....");
    }
    
    //之后執(zhí)行
    public void after(){
        System.out.println("StudentAOP.after");
    }
    
    //在之后執(zhí)行,只在沒有出現(xiàn)異常的時候執(zhí)行
    public void afterReturning(){
        System.out.println("StudentAOP.afterReturning");
    }
    
    //之后執(zhí)行,但是只在出現(xiàn)異常的時候執(zhí)行
    public void afterThrowing(){
        System.out.println("StudentAOP.throwing");
    }
    
    //環(huán)繞方法
    public void arounding(){
        System.out.println("StudentAOP.arounding");
    }
}
  • 定義StudentProxy代理類
/**
 * 靜態(tài)代理類
 * @author chenjiabing
 */
@Component
public class StudentProxy implements IStudentService {
    @Resource
    private StudentAOP studentAOP;   //依賴注入切面對象
    @Resource
    private IStudentService studentService;  //目標對象
    
    public void add() {
        try {
            studentAOP.arounding();  //執(zhí)行環(huán)繞方法
            studentAOP.before();  //執(zhí)行切面類的方法
            studentService.add();  //執(zhí)行目標對象的方法
            studentAOP.after();  //執(zhí)行切面的after方法
            studentAOP.afterReturning();  //執(zhí)行切面類afterReturning的方法
        } catch (Exception e) {
            studentAOP.afterThrowing();  //執(zhí)行切面類的方法,在出現(xiàn)異常之后執(zhí)行
        }finally{
            studentAOP.arounding();  //執(zhí)行環(huán)繞方法
        }
    }
}
  • 測試方法
@Test
public void test1() {
    // 加載Spring的配置文件
    AbstractApplicationContext ac = new ClassPathXmlApplicationContext(
            "spring-dao.xml", "spring-service.xml","spring-aop.xml");
    
    //創(chuàng)建Service,其中使用的是動態(tài)代理類
    IStudentService studentService=ac.getBean("studentProxy",IStudentService.class);
    studentService.add();
}

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

代理類實現(xiàn)java.lang.reflect.InvocationHandler接口
/**
 * 動態(tài)代理的類
 * 
 * @author chenjiabing
 */
@Component   // 創(chuàng)建對象
public class ProxyHandler implements InvocationHandler {
    
    private Object object; // 目標對象
    
    @Resource
    private StudentAOP studentAOP; // 注入切面類

    // 獲取動態(tài)代理類的對象
    public Object getObject(Object object){
        this.object=object;
        /**
         * 第一個參數(shù):目標類的類加載器
         * 第二個參數(shù):目標類的接口
         * 第三個參數(shù):動態(tài)代理的實例
         */
        return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), this);
    }

    /**
     * @param proxy :被代理的對象
     * @param method : 要調(diào)用的方法
     * @param args : 方法調(diào)用的時候所需要的參數(shù)
     */
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        
        studentAOP.before();  //調(diào)用切面的before方法
        
        //通過反射調(diào)用目標類的方法
        Object result=method.invoke(object, args);  //調(diào)用目標類的方法
        studentAOP.after();  //調(diào)用切面的after方法
        
        return result;
    }

}
  • 測試方法
@Test
public void test2() {
    // 加載Spring的配置文件
    AbstractApplicationContext ac = new ClassPathXmlApplicationContext(
            "spring-dao.xml", "spring-service.xml","spring-aop.xml");
    
    //獲取動態(tài)代理類的對象
    ProxyHandler proxyHandler=ac.getBean("proxyHandler",ProxyHandler.class);
    
    //獲取代理類的對象
    IStudentService studentService=(IStudentService) proxyHandler.getObject(new StudentServiceImpl());
    studentService.add();
    
}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容