Transcript
Page 1: Moving From Plexus To Guice

Moving from to

Page 2: Moving From Plexus To Guice

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

● Single plexus.xml

● META-INF/plexus/components.xml (Class-Path)

Page 4: Moving From Plexus To Guice

● Has annotations, but still need to generate XML

Page 5: Moving From Plexus To Guice

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

JSR 330

● New standard for Java dependency injection

// Constructor injection

// Setter injection

// Field injection

Page 7: Moving From Plexus To Guice

● 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

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

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

Injection Listeners

Page 11: Moving From Plexus To Guice

-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

Compatibility Shim

Page 13: Moving From Plexus To Guice

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

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

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

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

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

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

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

Time for a Demonstration!

Page 21: Moving From Plexus To Guice

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

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

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

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

Next Steps

● How can we resolve this mismatch?

● Find out in "Dynamic Guice Applications" ...

Page 26: Moving From Plexus To Guice

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

Questions?

Page 28: Moving From Plexus To Guice

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


Top Related