Dynamic Guice Applications

22
Dynamic Applications

Transcript of Dynamic Guice Applications

Page 1: Dynamic Guice Applications

Dynamic Applications

Page 2: Dynamic Guice Applications

Why ?

● Standard: reference implementation for JSR 330

● Flexible: can map all sorts of metadata to bindings

● Type-safe: detailed messages when things go wrong

● Modular: multiple extensions available, OSGi-ready

Page 3: Dynamic Guice Applications

● Fluent Java binding API

● Records generic information lost during erasure

● Can be driven by Java / annotations / XML / ...etc...

Page 4: Dynamic Guice Applications

JSR 330

● New standard for Java dependency injection

// Constructor injection

// Setter injection

// Field injection

Page 5: Dynamic Guice Applications

Extending JSR 330

● JSR 330 tells us how to mark dependencies

● and qualify them (just like with Plexus hints)

● But it does not say how to mark components

Page 6: Dynamic Guice Applications

Identifying JSR 330 components

● Wrap the Class-Path up as a ClassSpace and scan it● Look for classes with qualifiers such as @Named

● Empty @Named means "use class name" instead● Binding type found by analysing class hierarchy

Page 7: Dynamic Guice Applications

What is a dynamic application?

● OSGi lets us dynamically add / remove bundles● ... without needing to restart the application● This may add / remove qualified components

● ... but Guice bindings are static

Page 8: Dynamic Guice Applications

Dynamic application vs. static bindings

● How can we resolve this mismatch?

● Need support for dynamic component collections!

Page 9: Dynamic Guice Applications

Injector Graph

Page 10: Dynamic Guice Applications

Bean Locator

Page 11: Dynamic Guice Applications

Dynamic Collections

● Backed by dynamic Iterable of qualified beans

● Iterables updated as injectors come and go

● Copy-on-iteration avoids synchronization overhead

● Weak-ref detects when Iterable not used anymore

Page 12: Dynamic Guice Applications

Qualified Dependencies

● Qualified Map

● Qualified Instance

Page 13: Dynamic Guice Applications

Hinted Dependencies

● Hinted Map

● Hinted List

Page 14: Dynamic Guice Applications

Dynamic Updates

● Passive collections are not enough

● Sometimes need active notifications

● Should we force use of an explicit API?

● ... or use reflection to scan for methods?

Page 15: Dynamic Guice Applications

Mediator Pattern

POJO

Mediator

Bean Locator add bean

remove bean

Page 16: Dynamic Guice Applications

Mediator Benefits

● Watching application bean can remain a POJO● Mediators detected just like any other component

● Created on-demand as bean instances created● Removed when the watching bean is GC'd

Page 17: Dynamic Guice Applications

Bootstrapping

● Just wrap your Class-Path as a ClassSpace

● ... install the QualifiedScannerModule

● and kick-start Guice injection as usual

Page 18: Dynamic Guice Applications

Time for a Demonstration!

Page 19: Dynamic Guice Applications

What about Peaberry?

● Majority of plugin bindings tied to its lifecycle

● Can simply create one injector per plugin

● For plugin services that are not tied to thelifecycle we need something more dynamic!

● Proxies backed by the OSGi Service Registry

Page 20: Dynamic Guice Applications

Links

JSR 330 http://code.google.com/p/atinject/

Guice http://code.google.com/p/google-guice/

Peaberry http://code.google.com/p/peaberry/

Spice-Inject http://svn.sonatype.org/spice/trunk/spice-inject

OSGi http://www.osgi.org/About/HowOSGi

Blog Updates http://www.sonatype.com/people/author/mcculls/

Page 21: Dynamic Guice Applications

Questions?

Page 22: Dynamic Guice Applications

Tr an sfo r m in g th e w ay so ftw are is m ad e .