Eva
Java粉絲募集中
  • 簡介
  • 我們的優勢
  • 最新消息
  • 下載與使用
  • 範例與說明
    • Eva Objects >
      • Bean類別定義
      • Entity型別定義
      • Struct型別定義
      • 建立Entity空白物件
      • 建立Entity修改物件
      • 建立Entity條件物件
      • 建立Struct值物件
    • Eva_ORM >
      • ORM程式主體
      • 使用條件物件執行操作
      • 執行更複雜條件的操作.
      • 分頁取回資料
      • 操作JDBC的參數
      • Array Property的操作
      • Collection Property的操作
      • 更複雜的Collection Property
      • 指定表格名稱
      • 新增資料庫Dialect
    • Eva_IOC >
      • IOC程式主體
      • AOP應用
    • Eva_Struct >
      • Struct範例
      • Bit Strcut範例
      • 指標範例
      • 於Eva_ORM中使用
    • Eva_Sort >
      • 依指定Properties排序
      • 部份排序
      • 優先排序部份
    • Eva_Cache >
      • hashing-based cache
      • 非hashing-based cache
  • API列表
  • 技術支援
    • 協助開發
  • 相關書籍

AOP應用.

在使用Eva_IOC管理物件的建立之後, 還可以再額外加入相關的Listener, 去接收物件建立時, 與物件方法被呼叫時的事件.
package com.logntw.eva.ioc;

import java.lang.reflect.Method;

import junit.framework.Assert;

import org.junit.Test;

import com.logntw.eva.anno.ioc.aop.Classes;
import com.logntw.eva.anno.ioc.aop.Methods;
import com.logntw.eva.anno.ioc.aop.WatchMethods;
import com.logntw.eva.ioc.aop.Advice;
import com.logntw.eva.ioc.aop.ObjectMonitor;

public class ObjectFactoryTest4
{
  @Test
  public void testNothing() throws Exception
  {
    Assert.assertTrue(true);
  }
  
  public static interface _XY
  {
    int _fun();
    
    int fun1(int x);

    int fun2(int x, int z);
    
    int fun3(int w, int x, int z);
  }
  
  public static class XY implements _XY
  {
    private final int y;      
    public XY(int y)
    {
      this.y = y;
    }
    
    @Override
    public int _fun()
    {      
      return y + 5;
    }
    
    public int fun1(int x)
    {
      return x + y + 10;
    }
    
    public int fun2(int x, int z)
    {
      return x + y + z + 20;
    }
    
    public int fun3(int w, int x, int z)
    {
      return w + x + y + z + 30; 
    }      
  }
  
  public static class XY2 
  {
    private final int y;
    public XY2(int y)
    {
      this.y = y;
    }

    public int fun2(int x, int z)
    {
      return x + y + z + 20;
    }
    
    public final int fun3(int w, int x, int z)
    {
      return w + x + y + z + 30; 
    }      
  }
  
  public static class XY3 implements _XY
  {
    private final int y;
    public XY3(int y)
    {
      this.y = y;
    }
    
    public int _fun()
    {      
      return y + 5;
    }

    final public int fun1(int x)
    {
      return x + y + 10;
    }
    
    public int fun2(int x, int z)
    {
      return x + y + z + 20;
    }
    
    public int fun3(int w, int x, int z)
    {
      return w + x + y + z + 30; 
    }  
    
    public int fun4(int v, int w, int x, int z)
    {
      return v + w + x + y + z + 40; 
    }
  }
  
  private static int c1 = 0;
  private static int c2 = 0;
  private static int c3 = 0;
  public static class XMonitor implements ObjectMonitor
  {
    @Override
    public void onMethodCannotBeMonitored(Method m)
    {    
      c1 ++;
    }

    @Override
    public void onClassCannotBeMonitored(Class clz)
    {
      c2 ++;      
    }  
    
    @WatchMethods(clz=@Classes(pattern="com.logntw.eva.ioc.ObjectFactoryTest4\\S*"), method=@Methods())    
    public void onMethodInvoked(Advice advice, int a)
    {
      c3 ++;
    }
    
  }
  
  private static int c4 = 0;
  private static int c5 = 0;
  private static int c6 = 0;  
  public static class YMonitor implements ObjectMonitor
  {
    @Override
    public void onMethodCannotBeMonitored(Method m)
    {
      c4++;      
    }

    @Override
    public void onClassCannotBeMonitored(Class clz)
    {
      c5++;    
    }  
    
    @WatchMethods(clz=@Classes(pattern="com.logntw.eva.ioc.ObjectFactoryTest4\\S*"), method=@Methods())  
    public void onMethodInvoked(Advice advice)
    {
      c6++;      
    }
  };
  
  private static int c7 = 0;
  private static int c8 = 0;
  private static int c9 = 0;  
  public static class ZMonitor implements ObjectMonitor
  {
    @Override
    public void onMethodCannotBeMonitored(Method m)
    {      
      c7++;      
    }

    @Override
    public void onClassCannotBeMonitored(Class clz)
    {
      c8++;    
    }  
    
    @WatchMethods(clz=@Classes(pattern="com.logntw.eva.ioc.ObjectFactoryTest4\\S*"), method=@Methods())  
    public void onMethodInvoked(Advice advice, Object... a)
    {
      c9++;    
    }
  };
  
  private static int c10 = 0;
  private static int c11 = 0;
  private static int c12 = 0;
  public static class WMonitor implements ObjectMonitor
  {
    @Override
    public void onMethodCannotBeMonitored(Method m)
    {      
      c10++;      
    }

    @Override
    public void onClassCannotBeMonitored(Class clz)
    {
      c11++;    
    }  
    
    @WatchMethods(clz=@Classes(pattern="com.logntw.eva.ioc.ObjectFactoryTest4\\S*"), method=@Methods())  
    public void onMethodInvoked(Advice advice, int a, Object... b)
    {
      c12++;    
      
      for(int i = 0 ; i < b.length; i++)
        Assert.assertTrue(b[i] instanceof Integer);
    }
  };
  
  
  @Test
  public void testObjectFactory() throws Exception
  {      
    ObjectFactory objectFactory = DefoObjectFactory.getInstance();
    objectFactory.addObjectMonitor(new XMonitor(), new YMonitor(), new ZMonitor(), new WMonitor());
    
          
    _XY a = (_XY)objectFactory.getInstance("com.logntw.eva.ioc.ObjectFactoryTest4$XY", 1);    
    Assert.assertEquals(a.fun1(2), 13);    
    Assert.assertEquals(a.fun2(2,3), 26);  
    Assert.assertEquals(c3, 1);
    Assert.assertEquals(c6, 0);
    Assert.assertEquals(c9, 2);
    Assert.assertEquals(c12, 2);
    
    Assert.assertEquals(c7, 0);
    XY2 b = (XY2)objectFactory.getInstance("com.logntw.eva.ioc.ObjectFactoryTest4$XY2", 2);
    Assert.assertEquals(c7, 0+1);
    
    XY3 c = (XY3)objectFactory.getInstance("com.logntw.eva.ioc.ObjectFactoryTest4$XY3", 3);
    Assert.assertEquals(c7, 0+1+1);
    Assert.assertEquals(c4, 0+0);
    Assert.assertEquals(c1, 1);
    
    Assert.assertEquals(b.fun3(1, 2, 3), 38);
    Assert.assertEquals(c9, 2);
    Assert.assertEquals(c.fun3(1, 2, 3), 39);
    Assert.assertEquals(c3, 1);
    Assert.assertEquals(c6, 0);
    Assert.assertEquals(c9, 3);
    Assert.assertEquals(c12, 3);
    Assert.assertEquals(c._fun(), 8);
    Assert.assertEquals(c6, 1);
    
    Assert.assertEquals(c9, 4);
    Assert.assertEquals(c12, 3);
  }  
}
Powered by Create your own unique website with customizable templates.