JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java Application Framework  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Guice 的 AOP 該如何使用呢?
qrtt1





發文: 1747
積分: 31
於 2007-04-04 00:37 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
弟仿造手冊上的例子製作了
interface: Client, Service
class: ClientImpl, ServiceImpl

Service 的介面與實體類別
1
2
3
public interface Service {
  public void go();
}


1
2
3
4
5
6
7
8
public class ServiceImpl implements Service{
 
  @AutoLog
  public void go() {
    System.out.println("Service is really going");
  }
 
}


Client 的介面與實體類別
1
2
3
4
5
6
7
import com.google.inject.Inject;
 
public interface Client {
 
  public void go();
 
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Stage;
 
public class ClientImpl implements Client {
 
  private final Service service;
 
  @Inject
  public ClientImpl(Service service) {
    this.service = service;
  }
 
  @AutoLog()
  public void go() {
    service.go();
  }
 
}


Module 介面實作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import static com.google.inject.matcher.Matchers.annotatedWith;
import static com.google.inject.matcher.Matchers.any;
 
import com.google.inject.Binder;
import com.google.inject.Module;
 
public class MyModule implements Module {
 
  public void configure(Binder binder) {
    binder.bind(Service.class).to(ServiceImpl.class);
    binder.bind(Client.class).to(ClientImpl.class);
    binder.bindInterceptor(any(), annotatedWith(AutoLog.class),
        new TracingInterceptor());
  }
 
}


AutoLog 註解
1
2
public @interface AutoLog {
}


AOP 的 MethodInterceptor 實作
1
2
3
4
5
6
7
8
9
10
11
12
import java.util.logging.Logger;
 
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
 
public class TracingInterceptor implements MethodInterceptor {
  
  public Object invoke(MethodInvocation i) throws Throwable {
    Logger.getAnonymousLogger().info("haha");
    return null;
  }
}


主程式 GuiceTaste
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Stage;
 
public class GuiceTaste {
  public static void main(String[] args) {
    Injector injector = Guice.createInjector(Stage.PRODUCTION,
        new Module[] { new MyModule() });
 
    injector.getInstance(Service.class);
    Client c1 = injector.getInstance(Client.class);
 
    c1.go();
 
  }
}


程式雖然可以跑,但是 c1.go() 是不是應該要觸發 MethodInterceptor 發生掉什麼事?
也許弟弄錯了什麼,所以結果只印出了 "Service is really going" 請知道門道的朋友帶路一下唄 Big Smile


qrtt1 edited on 2007-04-04 09:18
reply to postreply to post
蝸牛角上爭何事?石火光中寄此身,隨富隨貧且歡樂,不開口笑是癡人。
my notes
作者 Re:Guice 的 AOP 該如何使用呢? [Re:qrtt1]
linexpmail

好玩就好



發文: 456
積分: 15
於 2007-04-04 08:34 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不知是否缺 RetentionPolicy 的問題,因為沒提到 @Retention(RUNTIME) 的部份,
參考看看。


reply to postreply to post
不除妄想不求真
作者 Re:Guice 的 AOP 該如何使用呢? [Re:linexpmail]
qrtt1





發文: 1747
積分: 31
於 2007-04-04 09:17 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
linexpmail wrote:
不知是否缺 RetentionPolicy 的問題,因為沒提到 @Retention(RUNTIME) 的部份,
參考看看。


感謝您 Big Smile
原來是第一次使用 annotation 的我遺漏了重要的東西

1
2
3
4
5
6
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
 
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoLog {
}


雖然不知還少了什麼,不過目前是能 work


reply to postreply to post
蝸牛角上爭何事?石火光中寄此身,隨富隨貧且歡樂,不開口笑是癡人。
my notes
» JWorld@TW »  Java Application Framework

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8