moving from plexus to guice
TRANSCRIPT
Moving from to
Why move?
● Plexus is not widely used outside of Maven
● Lack of documentation, not always up-to-date
● Extra steps needed to generate Plexus XML
● Not type-safe, lacks support for generics
● Single plexus.xml
● META-INF/plexus/components.xml (Class-Path)
● Has annotations, but still need to generate XML
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
JSR 330
● New standard for Java dependency injection
// Constructor injection
// Setter injection
// Field injection
● Fluent Java binding API
● Records generic information lost during erasure
● Can be driven by Java / annotations / XML / ...etc...
Migration Path
● Preserve investment in legacy Plexus components● Allow gradual migration to JSR 330 annotations
● Must support mixture of Plexus and JSR 330
Customizing
● Guice provides an SPI to hook into injections
● TypeListeners called for matching bound types
● Register MembersListeners for custom injection
● Register InjectionListeners for custom lifecycles
● SPI supports binding introspection / rewriting
Injection Listeners
-y Beans
● Writing all those listener classes can get tedious● guice-bean library cuts out most of the work
● You provide a BeanBinder implementation
● that can provide PropertyBinders for each type
● which can supply PropertyBindings for each property
● Bean conventions select field / setter properties
Compatibility Shim
Metadata
● Canonical form of Plexus bindings
● Captures @Component implementations●
● Maps @Component bean properties to
● @Requirement settings
● @Configuration settings
● Interns Plexus hint strings to save space
Scanning for Metadata
● ClassSpace abstraction supplies classes / resources
● Can be backed by Plexus Classworlds or OSGi
● Plexus XML mapped into Plexus annotations
● Plexus annotations are interpolated and stored
Converting Configuration
● Builds on top of standard Guice TypeConverters
● Supports properties, nested collections, and beans● Guice SPI lets you add your own TypeConverters
Locating Components
● Guice bindings map Keys to Providers● Each Key has a type and optional annotation● Plexus components have roles and hints
Key == Class<Role> + @Named(hint)
● Uses Guice SPI to find component bindings
Binding Components
● Opposite of locating components
● @Component roles and hints turned into Keys
● Keys used to bind component implementations
● Singleton default, unless strategy is "per-lookup"
Injecting Requirements
● @Requirement locate Plexus components
● Handles Maps, Lists, instances, and Wildcards
● Loggers automatically configured and injected
● @Configuration convert constant using type
Managing Lifecycles
● Uses an InjectionListener to listen for bean instances
● Plexus lifecycle "personality" applied after injection
● Reverse lifecycle applied when container disposed
● Need to manage extra metadata (like descriptions)
Time for a Demonstration!
Classworlds
● Plexus Container API still depends on Classworlds
● But dependency is much less in guice-plexus-shim
● Just need to write classworlds-shim around OSGi
● ... to get Plexus apps running on OSGi containers
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
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
Next Steps
● We can now run Plexus apps on top of Guice● ... and start separating apps into OSGi bundles● OSGi lets us dynamically add / remove bundles
● ... but Guice bindings are static
Next Steps
● How can we resolve this mismatch?
● Find out in "Dynamic Guice Applications" ...
Links
JSR 330 http://code.google.com/p/atinject/
Guice http://code.google.com/p/google-guice/
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/
Questions?
Tr an sfo r m in g th e w ay so ftw are is m ad e .