Guice in Athena 
2014/11/28 datayes.com
• Request/Response Flow 
• Init 
• Guice 
– Motivation? 解决什么问题 
– 如何使用 
– 原理和机制(Inside)
request/response flow 
AthenaSer 
vlet 
doGet(res, resp) 
Server 
handle
request/response flow 
GuiceFilter LogFilter JsonpFilter 
GuiceCont 
ainer 
HttpServlet(servlet) 
ServletContainer (Jersey) 
NewsReso 
urce 
service(res, resp) 
doFilter(res, resp) 
doFilter(res, resp) 
doFilter(res, resp) 
Server 
handle 
dispatch
Request/Response Stack
No new instance 
Jersey 
Guice new NewsResource 
new NewsMapper
Init 
Server GuiceServletCo 
doStart 
ntextListener 
contextInitialized 
getInjector 
Guice 
createInjector 
ServletContextListener(servlet) 
InternalInjector 
Creator 
build 
ConstructorInje 
ctor 
construct 
MembersInject 
or 
NewsResource() 
loadEagerSingletons 
NewsReso 
urce 
memberInject 
construct
Init
Guice : Motivation 
• 我们遇到的问题 
– Reused code seems hard 
– Class and Package boundary/layer is vague/fuzzy
Guice : Motivation 
• Why Guice? 
Elasticsearch heavily build on Guice 
InternalNode.java
Guice : Motivation 
• dependency injection framework 
• Easier to 
– change 
– unit test 
– reuse in other contexts 
• Modularity
Guice : Modularity 
• Modularity 
– Elasticsearch
Guice : Modularity 
• Modularity 
– Elasticsearch
Guice : Modularity
Guice : Modularity
Guice : principle (目的) 
• Separate implementation from interface 
• Decouple concrete from concrete 
• IOC principle 
– reusable code and the problem-specific code are developed 
independently 
• Dependency Inversion principle 
– High-level modules should not depend on low-level 
modules. Both should depend on abstractions. 
– Abstractions should not depend on details. Details should 
depend on abstractions.
Guice : DI (手段) 
1 
3 
2 
4 
DI : client 
service/service impl 
injector
Guice : 如何使用 
• 1) Inject 
– Constructor 
– Method 
– Field 
Athena 中主要是Field,如果构造函数中使用依赖,则到则使用Constructor
Guice : 如何使用 
• 2) Binding 
– Linked Bindings 
• bindings map a type to its implementation 
– Untargeted Bindings 
• concrete classes and types annotated by either 
@ImplementedBy or @ProvidedBy 
– Binding Annotations
Guice : 如何使用 
• 3) Module 
– A module is a collection of bindings 
– Install other modules 
代码模块化 
M 
install 
M1 M2 M3 
type1 
type2 
bind 
type3
Guice : 如何使用 
• 4) Injector 
– 传入一个/多个Module 
Athena -> GuiceServletConfig
Guice : other 
• Scopes 
– @singleton 
– @ RequestScoped 
– @ SessionScoped 
• AOP 
– MethodInterceptor
Guice : Inside 
• Injector 
– Graph construction 
– Binding Resolution 
– ClassLoader 
• AOP 
– Reflection vs. codegen
Reference 
• https://www.youtube.com/watch?v=hBVJbzAa 
gfs 
• http://www.infoq.com/presentations/Dagger

Guice in athena

  • 1.
    Guice in Athena 2014/11/28 datayes.com
  • 2.
    • Request/Response Flow • Init • Guice – Motivation? 解决什么问题 – 如何使用 – 原理和机制(Inside)
  • 3.
    request/response flow AthenaSer vlet doGet(res, resp) Server handle
  • 4.
    request/response flow GuiceFilterLogFilter JsonpFilter GuiceCont ainer HttpServlet(servlet) ServletContainer (Jersey) NewsReso urce service(res, resp) doFilter(res, resp) doFilter(res, resp) doFilter(res, resp) Server handle dispatch
  • 5.
  • 6.
    No new instance Jersey Guice new NewsResource new NewsMapper
  • 7.
    Init Server GuiceServletCo doStart ntextListener contextInitialized getInjector Guice createInjector ServletContextListener(servlet) InternalInjector Creator build ConstructorInje ctor construct MembersInject or NewsResource() loadEagerSingletons NewsReso urce memberInject construct
  • 8.
  • 9.
    Guice : Motivation • 我们遇到的问题 – Reused code seems hard – Class and Package boundary/layer is vague/fuzzy
  • 10.
    Guice : Motivation • Why Guice? Elasticsearch heavily build on Guice InternalNode.java
  • 11.
    Guice : Motivation • dependency injection framework • Easier to – change – unit test – reuse in other contexts • Modularity
  • 12.
    Guice : Modularity • Modularity – Elasticsearch
  • 13.
    Guice : Modularity • Modularity – Elasticsearch
  • 14.
  • 15.
  • 16.
    Guice : principle(目的) • Separate implementation from interface • Decouple concrete from concrete • IOC principle – reusable code and the problem-specific code are developed independently • Dependency Inversion principle – High-level modules should not depend on low-level modules. Both should depend on abstractions. – Abstractions should not depend on details. Details should depend on abstractions.
  • 17.
    Guice : DI(手段) 1 3 2 4 DI : client service/service impl injector
  • 18.
    Guice : 如何使用 • 1) Inject – Constructor – Method – Field Athena 中主要是Field,如果构造函数中使用依赖,则到则使用Constructor
  • 19.
    Guice : 如何使用 • 2) Binding – Linked Bindings • bindings map a type to its implementation – Untargeted Bindings • concrete classes and types annotated by either @ImplementedBy or @ProvidedBy – Binding Annotations
  • 20.
    Guice : 如何使用 • 3) Module – A module is a collection of bindings – Install other modules 代码模块化 M install M1 M2 M3 type1 type2 bind type3
  • 21.
    Guice : 如何使用 • 4) Injector – 传入一个/多个Module Athena -> GuiceServletConfig
  • 22.
    Guice : other • Scopes – @singleton – @ RequestScoped – @ SessionScoped • AOP – MethodInterceptor
  • 23.
    Guice : Inside • Injector – Graph construction – Binding Resolution – ClassLoader • AOP – Reflection vs. codegen
  • 24.
    Reference • https://www.youtube.com/watch?v=hBVJbzAa gfs • http://www.infoq.com/presentations/Dagger

Editor's Notes

  • #12 https://github.com/google/guice
  • #13 https://github.com/google/guice
  • #14 https://github.com/google/guice
  • #19 https://github.com/google/guice/wiki/Injections