google guice
DESCRIPTION
10-th meetuo of Java User Group of LvivTRANSCRIPT
Google guiceGoogle guice
It’s easy It’s easy
© Skelia 2011
Agenda
© Skelia 2011 2
●Pattern Dependency injection
●Why we need DI●Google guice introduction●Guice simple injection ●Integration with Servlets ●Integration with Guice●Philosophy●Instead of summary●Q&A
DI or Don't Call Me, I Will Call You
© Skelia 2011 3
● Dependency injection (DI) in object-oriented computer programming is a technique that indicates to a part of a program which other parts it can use, i.e. to supply an external dependency – a reference – to a software component. In technical terms, it is a design pattern that separates behavior from dependency resolution, thus decoupling highly dependent components.
Wikipedia: http://en.wikipedia.org/wiki/Dependency_injection
DI or Don't Call Me, I Will Call You
© Skelia 2011 4
● Dependency injection (DI) in object-oriented computer programming is a technique that indicates to a part of a program which other parts it can use, i.e. to supply an external dependency – a reference – to a software component. In technical terms, it is a design pattern that separates behavior from dependency resolution, thus decoupling highly dependent components.
Wikipedia: http://en.wikipedia.org/wiki/Dependency_injection
DI or Don't Call Me, I Will Call You
© Skelia 2011 6
Wild programmer
Spaghetti Architecture
Google Guice
© Skelia 2011 7
Google-Guice
Current version 3.0
Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 5 and
above, brought to you by Google.
Google Guice (Module)
© Skelia 2011 9
Module Types:•Non-obvious
•AbstractModule
•ServletModule
© Skelia 2011 10
Binding types:
• Bind implementationbind(PaymentCashImpl.class)
•Bind implementation to interface bind(Payment.class).to(PaymentCardImpl.class)
•Bind multiple implementation to interfacebind(Payment.class).annotatedWith(Cash.class).to(PaymentCashImpl.class)
bind(Payment.class).annotatedWith(Names.named("Cash")).to(PaymentCashImpl.class)
Google Guice (Binding)
© Skelia 2011 11
Google Guice (Injection)
Injection types :
• Constructor
• Method
• Field (not recommended)
@Injectprivate @Card Payment payment;
@Named("PaymentType")@Injectprivate String text;
Google Guice (Provides)
© Skelia 2011 12
public class PayModule extends AbstractModule{ protected void configure() { }
@Provides Payment providePayment(){ CashPayment pay = new CashPayment(); pay.setAmount(100); return pay; }@Provides List<String> providesListOfString() { return new ArrayList<String>();
}
@Injectpublic void setList(List<String> list)
Google Guice (Scope)
© Skelia 2011 13
Google Guice (AOP)
© Skelia 2011 14
Google Guice (Sample)
© Skelia 2011 15
Google Guice (Servlet integration)
© Skelia 2011 16
Google Guice (Sample)
© Skelia 2011 17
Google Guice (Integration)
© Skelia 2011 18
MobileRoboGuice - Guice for Android.
Other Languages•Groovy-Guice - Groovy•Ninject - .NET•Snake Guice - Python•Smartypants-IOC - Flex/AS3•Dawn - AS3
Google Guice (Integration)
© Skelia 2011 19
Google Guice (philosophy)
© Skelia 2011 20
Google Guice (philosophy)
© Skelia 2011 21
Instead of Conclusion
© Skelia 2011 22
© Skelia 2011 23
Thank you !