google guice

23
Google guice Google guice It’s easy It’s easy © Skelia 2011

Upload: andriy-andrunevchyn

Post on 06-May-2015

4.288 views

Category:

Technology


4 download

DESCRIPTION

10-th meetuo of Java User Group of Lviv

TRANSCRIPT

Page 1: Google Guice

Google guiceGoogle guice

It’s easy It’s easy

© Skelia 2011

Page 2: Google Guice

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

Page 3: Google Guice

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

Page 4: Google Guice

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

Page 5: Google Guice

DI or Don't Call Me, I Will Call You

© Skelia 2011 5

Page 6: Google Guice

DI or Don't Call Me, I Will Call You

© Skelia 2011 6

Wild programmer

Spaghetti Architecture

Page 7: Google Guice

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.

Page 8: Google Guice

Google Guice

© Skelia 2011 8

Page 9: Google Guice

Google Guice (Module)

© Skelia 2011 9

Module Types:•Non-obvious

•AbstractModule

•ServletModule

Page 10: Google Guice

© 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)

Page 11: Google Guice

© Skelia 2011 11

Google Guice (Injection)

Injection types :

• Constructor

• Method

• Field (not recommended)

@Injectprivate @Card Payment payment;

@Named("PaymentType")@Injectprivate String text;

Page 12: Google Guice

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)

Page 13: Google Guice

Google Guice (Scope)

© Skelia 2011 13

Page 14: Google Guice

Google Guice (AOP)

© Skelia 2011 14

Page 15: Google Guice

Google Guice (Sample)

© Skelia 2011 15

Page 16: Google Guice

Google Guice (Servlet integration)

© Skelia 2011 16

Page 17: Google Guice

Google Guice (Sample)

© Skelia 2011 17

Page 18: Google Guice

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

Page 19: Google Guice

Google Guice (Integration)

© Skelia 2011 19

Page 20: Google Guice

Google Guice (philosophy)

© Skelia 2011 20

Page 21: Google Guice

Google Guice (philosophy)

© Skelia 2011 21

Page 22: Google Guice

Instead of Conclusion

© Skelia 2011 22

Page 23: Google Guice

© Skelia 2011 23

Thank you !