moving from plexus to guice
TRANSCRIPT
![Page 1: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/1.jpg)
Moving from to
![Page 2: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/2.jpg)
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
![Page 3: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/3.jpg)
● Single plexus.xml
● META-INF/plexus/components.xml (Class-Path)
![Page 4: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/4.jpg)
● Has annotations, but still need to generate XML
![Page 5: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/5.jpg)
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 6: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/6.jpg)
JSR 330
● New standard for Java dependency injection
// Constructor injection
// Setter injection
// Field injection
![Page 7: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/7.jpg)
● Fluent Java binding API
● Records generic information lost during erasure
● Can be driven by Java / annotations / XML / ...etc...
![Page 8: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/8.jpg)
Migration Path
● Preserve investment in legacy Plexus components● Allow gradual migration to JSR 330 annotations
● Must support mixture of Plexus and JSR 330
![Page 9: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/9.jpg)
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
![Page 10: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/10.jpg)
Injection Listeners
![Page 11: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/11.jpg)
-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
![Page 12: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/12.jpg)
Compatibility Shim
![Page 13: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/13.jpg)
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
![Page 14: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/14.jpg)
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
![Page 15: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/15.jpg)
Converting Configuration
● Builds on top of standard Guice TypeConverters
● Supports properties, nested collections, and beans● Guice SPI lets you add your own TypeConverters
![Page 16: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/16.jpg)
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
![Page 17: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/17.jpg)
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"
![Page 18: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/18.jpg)
Injecting Requirements
● @Requirement locate Plexus components
● Handles Maps, Lists, instances, and Wildcards
● Loggers automatically configured and injected
● @Configuration convert constant using type
![Page 19: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/19.jpg)
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)
![Page 20: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/20.jpg)
Time for a Demonstration!
![Page 21: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/21.jpg)
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
![Page 22: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/22.jpg)
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 23: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/23.jpg)
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 24: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/24.jpg)
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
![Page 25: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/25.jpg)
Next Steps
● How can we resolve this mismatch?
● Find out in "Dynamic Guice Applications" ...
![Page 26: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/26.jpg)
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/
![Page 27: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/27.jpg)
Questions?
![Page 28: Moving From Plexus To Guice](https://reader033.vdocument.in/reader033/viewer/2022052413/55982ce11a28abc0088b45bc/html5/thumbnails/28.jpg)
Tr an sfo r m in g th e w ay so ftw are is m ad e .