<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sonatype Blog &#187; plexus</title>
	<atom:link href="http://blog.sonatype.com/people/tag/plexus/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sonatype.com/people</link>
	<description>Sonatype is transforming software development with tools, information and services that enable organizations to build better software, faster, using open-source components.</description>
	<lastBuildDate>Thu, 16 May 2013 18:53:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>From Plexus to Guice (#3): Creating a Guice Bean Extension Layer</title>
		<link>http://blog.sonatype.com/people/2010/01/from-plexus-to-guice-3-creating-a-guice-bean-extension-layer/</link>
		<comments>http://blog.sonatype.com/people/2010/01/from-plexus-to-guice-3-creating-a-guice-bean-extension-layer/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 09:00:53 +0000</pubDate>
		<dc:creator>Stuart McCulloch</dc:creator>
				<category><![CDATA[Maven]]></category>
		<category><![CDATA[Sonatype]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[plexus]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=3981</guid>
		<description><![CDATA[In the first two articles in this series you learned why Sonatype has decided to move our development efforts from Plexus to Guice and you&#8217;ve also been introduced to Sonatype&#8217;s efforts to create a Guice/Plexus integration library to make the migration from Plexus to Guice seamless.   In this post, I&#8217;m going to dive into [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.sonatype.com/people/wp-content/uploads/2010/01/from-plexus-to-guice1.png"><img class="alignright size-full wp-image-4024" title="from-plexus-to-guice" src="http://www.sonatype.com/people/wp-content/uploads/2010/01/from-plexus-to-guice1.png" alt="" width="250" height="62" /></a>In the first two articles in this series you learned why<a href="http://www.sonatype.com/people/2010/01/from-plexus-to-guice-1-why-guice/"> Sonatype has decided to move our development efforts from Plexus to Guice</a> and you&#8217;ve also been introduced to Sonatype&#8217;s efforts to create a <a href="http://www.sonatype.com/people/2010/01/from-plexus-to-guice-2-the-guiceplexus-bridge-and-custom-bean-injection/">Guice/Plexus integration library</a> to make the migration from Plexus to Guice seamless.   In this post, I&#8217;m going to dive into the details of bean injection in Guice, and I&#8217;m going to demonstrate how you can use the general-purpose bean injection layer in the Guice/Plexus integation library to intialize a bean.  This post is highly technical and is meant for developers interested in the details of our attempts to bridge Plexus and Guice.<span id="more-3981"></span></p>

<p><strong>Guice-Bean Extension Layer</strong></p>

<p>Our goal is to create a general-purpose bean injection layer on top of the raw <a title="2. Customizing Guice Injection" href="https://docs.sonatype.com/display/OSGI/2.+Customizing+Guice+Injection">custom injection</a> API provided by Guice. This layer will take care of registering the necessary <tt>MembersInjectors</tt> as well as scanning class hierarchies for potential bean properties. All the client needs to do is decide whether or not to supply a binding for a given property.  The primary class in this layer is <tt>BeanListener</tt>:</p>

<div>
<div><strong>org.sonatype.guice.bean.inject.BeanListener</strong></div>
<div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BeanListener
    <span style="color: #000000; font-weight: bold;">implements</span> TypeListener
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> BeanListener<span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">final</span> BeanBinder beanBinder <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// ...etc...</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>



</div>
</div>

<p>This is a <tt>TypeListener</tt> implementation that you can register with Guice to provide bean injection for any type. It accepts a single argument of type <tt>BeanBinder</tt>, which the client is expected to implement:</p>

<div>
<div><strong>org.sonatype.guice.bean.inject.BeanBinder</strong></div>
<div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> BeanBinder
<span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>B<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> PropertyBinder bindBean<span style="color: #009900;">&#40;</span> TypeLiteral<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>B<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> type, TypeEncounter<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>B<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> encounter <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>



</div>
</div>

<p>The single <tt>bindBean</tt> method should supply a <tt>PropertyBinder</tt> based on the type of bean being injected. This means clients can customize or cache information on a per-type basis, or simply provide the same <tt>PropertyBinder</tt> implementation for all bean types. Returning <tt>null</tt> will disable bean injection for that type.  The <tt>PropertyBinder</tt> interface does the same job as <tt>BeanBinder</tt>, but at the property level:</p>

<div>
<div><strong>org.sonatype.guice.bean.inject.PropertyBinder</strong></div>
<div>
<pre>public interface PropertyBinder
{
    &lt;T&gt; PropertyBinding bindProperty( BeanProperty&lt;T&gt; property );
}</pre>
</div>
</div>

<p>This is the point where clients decide whether or not to bind a particular property. They can either return an implementation of <tt>PropertyBinding</tt> which should inject the property, or return <tt>null</tt> to ignore this property. This process continues until all the bean properties have been scanned &#8211; unless the client returns the <tt>LAST_BINDING</tt> constant from the <tt>PropertyBinder</tt> API, which will immediately stop the scanning for the current bean. <tt>LAST_BINDING</tt> is supposed to be used by binders who know they can&#8217;t supply any more bindings, because then there&#8217;s no point in continuing the scan.  So what does a <tt>PropertyBinding</tt> look like?</p>

<div>
<div><strong>org.sonatype.guice.bean.inject.PropertyBinding</strong></div>
<div>
<pre>public interface PropertyBinding
{
    &lt;B&gt; void injectProperty( B bean );
}</pre>
</div>
</div>

<p>Well, not much as you can see! It has a single method <tt>injectProperty</tt> which is the trigger for injecting a value into the appropriate property of the given bean. How this value is created and how it is injected into the bean is up to the client, but most clients will use the <tt>BeanProperty</tt> supplied in the original <tt>bindProperty</tt> method to do the actual injection:</p>

<div>
<div><strong>org.sonatype.guice.bean.reflect.BeanProperty</strong></div>
<div>
<pre>public interface BeanProperty&lt;T&gt;
{
    &lt;A extends Annotation&gt; A getAnnotation( Class&lt;A&gt; annotationType );

    TypeLiteral&lt;T&gt; getType();

    String getName();

    &lt;B&gt; void set( B bean, T value );
}</pre>
</div>
</div>

<p>Specifically the <tt>set</tt> method lets you inject a value into this property for a given bean instance. Notice how you can also query the generic type, name, as well as any annotations attached to the bean property. You can use any or all of this information to determine what value to inject into the property: for example by querying XML metadata, runtime annotations, maybe even pulling values out of a database.</p>

<div>
<table><colgroup> <col width="24"></col> <col></col> </colgroup>
<tbody>
<tr>
<td valign="top"><img src="https://docs.sonatype.com/images/icons/emoticons/information.gif" border="0" alt="" width="16" height="16" align="absmiddle" /></td>
<td>Property names are normalized &#8211; a setter method called &#8220;setValue&#8221; has a property name of &#8220;value&#8221;</td>
</tr>
</tbody>
</table>
</div>

<div>
<table><colgroup> <col width="24"></col> <col></col> </colgroup>
<tbody>
<tr>
<td valign="top"><img src="https://docs.sonatype.com/images/icons/emoticons/information.gif" border="0" alt="" width="16" height="16" align="absmiddle" /></td>
<td>The <tt>BeanProperty</tt> currently doesn&#8217;t tell you whether it&#8217;s backed by a field or setter method. This is because we don&#8217;t need this information in our use-case and don&#8217;t believe others will, but some sort of query method could be added in the future if necessary.</td>
</tr>
</tbody>
</table>
</div>

<p>Enough talk, let&#8217;s actually try and use this extension layer in a small example!</p>

<h3>Example</h3>

<p>We begin by creating an example Maven project:</p>

<div>
<div>
<pre>mvn archetype:create "-DgroupId=example.bean.guice" "-DartifactId=guice-bean-example"

cd guice-bean-example/</pre>
</div>
</div>

<p>Remove the example App files, as we&#8217;ll be adding our own files soon:</p>

<div>
<div>
<pre>rm src/main/java/example/bean/guice/App.java
rm src/test/java/example/bean/guice/AppTest.java</pre>
</div>
</div>

<p>Now edit the POM, first we need to add a dependency to the <tt>guice-bean-inject</tt> library:</p>

<div>
<div><strong>pom.xml</strong></div>
<div>
<pre>  &lt;dependencies&gt;
    &lt;!-- ... --&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.sonatype.spice.inject&lt;/groupId&gt;
      &lt;artifactId&gt;guice-bean-inject&lt;/artifactId&gt;
      &lt;version&gt;0.1.0&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;!-- ... --&gt;
  &lt;/dependencies&gt;</pre>
</div>
</div>

<div>
<table><colgroup> <col width="24"></col> <col></col> </colgroup>
<tbody>
<tr>
<td valign="top"><img src="https://docs.sonatype.com/images/icons/emoticons/information.gif" border="0" alt="" width="16" height="16" align="absmiddle" /></td>
<td>This will also pull in the <tt>guice-bean-reflect</tt> and <tt>guice-patches</tt> libraries</td>
</tr>
</tbody>
</table>
</div>

<p>We also need to tell Maven to compile at the Java5 level, because this is the minimum required by Guice:</p>

<div>
<div><strong>pom.xml</strong></div>
<div>
<pre>  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
        &lt;configuration&gt;
          &lt;source&gt;1.5&lt;/source&gt;
          &lt;target&gt;1.5&lt;/target&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;</pre>
</div>
</div>

<p>We&#8217;re now ready to add our example code, let&#8217;s start with a very simple &#8220;do nothing&#8221; binder:</p>

<div>
<div><strong>src/main/java/example/bean/guice/SimpleBeanBinder.java</strong></div>
<div>
<pre>package example.bean.guice;

import org.sonatype.guice.bean.inject.BeanBinder;
import org.sonatype.guice.bean.inject.PropertyBinder;

import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;

public class SimpleBeanBinder
  implements BeanBinder
{
  public &lt;B&gt; PropertyBinder bindBean( TypeLiteral&lt;B&gt; type, TypeEncounter&lt;B&gt; encounter )
  {
    return null;
  }
}</pre>
</div>
</div>

<p>Well that&#8217;s fairly useless! We should at least return some property binders which in turn return some property bindings, otherwise nothing will be injected. To save on typing we&#8217;ll put our injection code in a couple of nested anonymous classes, but we could just as well have created named classes. Let&#8217;s also use the bean property to do the actual injection (for the moment we will just inject <tt>null</tt>).</p>

<div>
<div><strong>src/main/java/example/bean/guice/SimpleBeanBinder.java</strong></div>
<div>
<pre>package example.bean.guice;

import org.sonatype.guice.bean.inject.BeanBinder;
import org.sonatype.guice.bean.inject.PropertyBinder;
import org.sonatype.guice.bean.inject.PropertyBinding;
import org.sonatype.guice.bean.reflect.BeanProperty;

import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;

public class SimpleBeanBinder
  implements BeanBinder
{
  public &lt;B&gt; PropertyBinder bindBean( TypeLiteral&lt;B&gt; type, TypeEncounter&lt;B&gt; encounter )
  {
    return new PropertyBinder()
    {
      public &lt;T&gt; PropertyBinding bindProperty( final BeanProperty&lt;T&gt; property )
      {
        return new PropertyBinding()
        {
          public &lt;C&gt; void injectProperty( C bean )
          {
            property.set( bean, null );
          }
        };
      }
    };
  }
}</pre>
</div>
</div>

<div>
<table><colgroup> <col width="24"></col> <col></col> </colgroup>
<tbody>
<tr>
<td valign="top"><img src="https://docs.sonatype.com/images/icons/emoticons/information.gif" border="0" alt="" width="16" height="16" align="absmiddle" /></td>
<td>We made the <tt>property</tt> parameter <tt>final</tt> so we could use it inside the anonymous class</td>
</tr>
</tbody>
</table>
</div>

<p>Next we need to provide actual values for our bean properties. We&#8217;re free to get these values from anywhere we want, but to keep this example simple we&#8217;ll just get them from the Guice injector. So how do we access the injector while we&#8217;re still configuring bindings? Well the <tt>TypeEncounter</tt> has methods to look up the <tt>Provider</tt> for a given binding <tt>Key</tt>. But be careful &#8211; we can&#8217;t use these providers during the property binding call because the injector is still being initialized. We can only use them once the injection phase starts, such as in the <tt>injectProperty</tt> method.</p>

<p>Our example bean binder takes the simple name of the bean class, appends the property name, and turns it into a JSR 330 binding annotation like <tt>@Named("PersonBean.name")</tt>. <em>In practice you&#8217;ll also want to include the package name to avoid collisions.</em> The appropriate value provider is supplied by the <tt>TypeEncounter</tt> using a key based on the binding annotation and property type. We store the provider in a final variable so we can access it later on when we need to inject the property value into a bean.</p>

<div>
<div><strong>src/main/java/example/bean/guice/SimpleBeanBinder.java</strong></div>
<div>
<pre>package example.bean.guice;

import javax.inject.Named;
import javax.inject.Provider;

import org.sonatype.guice.bean.inject.BeanBinder;
import org.sonatype.guice.bean.inject.PropertyBinder;
import org.sonatype.guice.bean.inject.PropertyBinding;
import org.sonatype.guice.bean.reflect.BeanProperty;

import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.util.Jsr330;

public class SimpleBeanBinder
  implements BeanBinder
{
  public &lt;B1&gt; PropertyBinder bindBean( final TypeLiteral&lt;B1&gt; type, final TypeEncounter&lt;B1&gt; encounter )
  {
    return new PropertyBinder()
    {
      public &lt;T&gt; PropertyBinding bindProperty( final BeanProperty&lt;T&gt; property )
      {
        // simple mapping id, not guaranteed to be unique as we don't include the package namespace
        Named valueId = Jsr330.named( type.getRawType().getSimpleName() + "." + property.getName() );
        final Provider&lt;T&gt; valueProvider = encounter.getProvider( Key.get( property.getType(), valueId ) );

        return new PropertyBinding()
        {
          public &lt;C&gt; void injectProperty( C bean )
          {
            property.set( bean, valueProvider.get());
          }
        };
      }
    };
  }
}</pre>
</div>
</div>

<p>We now have a working bean binder that maps bean properties to JSR 330 named bindings. To finish things off, let&#8217;s add a bit of logging so we can see what the binder is doing during the test:</p>

<div>
<div><strong>src/main/java/example/bean/guice/SimpleBeanBinder.java</strong></div>
<div>
<pre>package example.bean.guice;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Named;
import javax.inject.Provider;

import org.sonatype.guice.bean.inject.BeanBinder;
import org.sonatype.guice.bean.inject.PropertyBinder;
import org.sonatype.guice.bean.inject.PropertyBinding;
import org.sonatype.guice.bean.reflect.BeanProperty;

import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.util.Jsr330;

public class SimpleBeanBinder
  implements BeanBinder
{
  private final Logger LOGGER = Logger.getLogger( SimpleBeanBinder.class.getName() );

  void log( String message )
  {
    // blank method+class to remove clutter <img src='http://blog.sonatype.com/people/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> 
    LOGGER.logp( Level.INFO, "", "", message );
  }

  public &lt;B1&gt; PropertyBinder bindBean( final TypeLiteral&lt;B1&gt; type, final TypeEncounter&lt;B1&gt; encounter )
  {
    log( "Binding bean &lt;" + type + "&gt;" );
    return new PropertyBinder()
    {
      public &lt;T&gt; PropertyBinding bindProperty( final BeanProperty&lt;T&gt; property )
      {
        log( "Binding property [" + property.getName() + "]" );

        // simple mapping id, not guaranteed to be unique as we don't include the package namespace
        Named valueId = Jsr330.named( type.getRawType().getSimpleName() + "." + property.getName() );
        final Provider&lt;T&gt; valueProvider = encounter.getProvider( Key.get( property.getType(), valueId ) );

        return new PropertyBinding()
        {
          public &lt;C&gt; void injectProperty( C bean )
          {
            T value = valueProvider.get();
            log( "Injecting property [" + property.getName() + "] with \"" + value + "\"" );
            property.set( bean, value );
          }
        };
      }
    };
  }
}</pre>
</div>
</div>

<p>OK so we have our simple bean binder, now we need a bean to test it with. How about the following:</p>

<div>
<div><strong>src/main/java/example/bean/guice/PersonBean.java</strong></div>
<div>
<pre>package example.bean.guice;

public class PersonBean
{
  private int age;

  private String name;

  public void setName( String name )
  {
    this.name = name.toUpperCase();
  }

  public String getName()
  {
    return name;
  }

  public int getAge()
  {
    return age;
  }
}</pre>
</div>
</div>

<div>
<table><colgroup> <col width="24"></col> <col></col> </colgroup>
<tbody>
<tr>
<td valign="top"><img src="https://docs.sonatype.com/images/icons/emoticons/forbidden.gif" border="0" alt="" width="16" height="16" align="absmiddle" /></td>
<td>This is a trivial bean that mixes field and setter injection, it is definitely not meant to represent good coding practice!</td>
</tr>
</tbody>
</table>
</div>

<p>Finally we need to write a test that exercises our bean binder. The following jUnit test bootstraps a Guice injector that uses our binder along with a couple of constant bindings for each of the bean properties. Guice will handle converting the constant strings to the right types. The injector is used to inject the example bean instance into the test, where its properties are checked against the expected values.</p>

<div>
<div><strong>src/test/java/example/bean/guice/PersonBeanTest.java</strong></div>
<div>
<pre>package example.bean.guice;

import javax.inject.Inject;

import junit.framework.TestCase;

import org.sonatype.guice.bean.inject.BeanListener;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.TypeLiteral;
import com.google.inject.matcher.Matchers;
import com.google.inject.util.Jsr330;

public class PersonBeanTest
  extends TestCase
{
  @Inject
  PersonBean personBean;

  @Override
  protected void setUp()
  {
    Guice.createInjector( new AbstractModule()
    {
      @Override
      protected void configure()
      {
        bindListener( Matchers.only( TypeLiteral.get( PersonBean.class ) ),
                      new BeanListener( new SimpleBeanBinder() ) );

        bindConstant().annotatedWith( Jsr330.named( "PersonBean.name" ) ).to( "John Smith" );
        bindConstant().annotatedWith( Jsr330.named( "PersonBean.age" ) ).to( "42" );
      }
    } ).injectMembers( this );
  }

  public void testNameBean()
  {
    // check the name was capitalized by the setter
    assertEquals( "JOHN SMITH", personBean.getName() );
    assertEquals( 42, personBean.getAge() );
  }
}</pre>
</div>
</div>

<div>
<table><colgroup> <col width="24"></col> <col></col> </colgroup>
<tbody>
<tr>
<td valign="top"><img src="https://docs.sonatype.com/images/icons/emoticons/information.gif" border="0" alt="" width="16" height="16" align="absmiddle" /></td>
<td>If you are doing a lot of Guice testing, you should take a look at the <a rel="nofollow" href="http://code.google.com/p/atunit/">atunit<sup><img src="https://docs.sonatype.com/images/icons/linkext7.gif" border="0" alt="" width="7" height="7" align="absmiddle" /></sup></a> library</td>
</tr>
</tbody>
</table>
</div>

<p>We&#8217;re now ready to test our example bean binder! Type the following command to kick things off:</p>

<div>
<div>
<pre>mvn clean install</pre>
</div>
</div>

<p>You should see the usual Maven output followed by a successful test, which should look something like this:</p>

<div>
<div>
<pre>-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running example.bean.guice.PersonBeanTest
Jan 8, 2010 12:27:07 AM
INFO: Binding bean &lt;example.bean.guice.PersonBean&gt;
Jan 8, 2010 12:27:08 AM
INFO: Binding property [name]
Jan 8, 2010 12:27:08 AM
INFO: Binding property [age]
Jan 8, 2010 12:27:08 AM
INFO: Injecting property [age] with "42"
Jan 8, 2010 12:27:08 AM
INFO: Injecting property [name] with "John Smith"
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.785 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0</pre>
</div>
</div>

<p>Congratulations, you&#8217;ve just used Guice to initialize a plain old bean with no annotations at all!</p>

<h3>Next: Reflection</h3>

<p>Interested in how the <tt>BeanListener</tt> finds bean properties? We&#8217;ll take a closer look at this in the next post in this series which will cover Generic Bean reflection.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2010/01/from-plexus-to-guice-3-creating-a-guice-bean-extension-layer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Plexus Container Five Minute Tutorial</title>
		<link>http://blog.sonatype.com/people/2009/05/plexus-container-five-minute-tutorial/</link>
		<comments>http://blog.sonatype.com/people/2009/05/plexus-container-five-minute-tutorial/#comments</comments>
		<pubDate>Thu, 21 May 2009 12:00:00 +0000</pubDate>
		<dc:creator>Brian Demers</dc:creator>
				<category><![CDATA[Sonatype]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[plexus]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=2241</guid>
		<description><![CDATA[The goal of this blog is to show an updated and more involved example then what is currently located on the plexus site.  This will cover creating a couple of components and explain the different ways to inject your dependencies. This example assumes you are using maven 2 to make your life easier. Creating a [...]]]></description>
				<content:encoded><![CDATA[<p>The goal of this blog is to show an updated and more involved example then what is currently located on the <a href="http://plexus.codehaus.org/guides/quick-start/getting-started.html">plexus site</a>.  This will cover creating a couple of components and explain the different ways to inject your dependencies.  This example assumes you are using maven 2 to make your life easier.</p>

<p><span id="more-2241"></span></p>

<h3>Creating a Component Interface</h3>

<p>The first task to creating a component is to define its <em>role</em>. In Java, this usually takes the form of defining an interface with the functionality the component will expose.</p>

<p><strong>Note:</strong> Plexus does not strictly require you use an interface to define the role, however it is strongly recommended to help improve your application design.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">example</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Cheese
<span style="color: #009900;">&#123;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * Slices the cheese for apportioning onto crackers.
    * @param slices the number of slices
    */</span>
    <span style="color: #000066; font-weight: bold;">void</span> slice<span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> slices <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * Get the description of the aroma of the cheese.
    * @return the aroma
    */</span>
    <span style="color: #003399;">String</span> getAroma<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>That is it, nothing special, just an interface.  We will use the class name as the role, in this example it will be <code>Cheese.class</code></p>

<h3>Creating a Component Implementation</h3>

<p>Once an interface is declared, you need to create one or more implementations of the functionality declared by it.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">example</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.codehaus.plexus.component.annotations.Component</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * All we need to tell Plexus that this is a component.
 */</span>
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span> role <span style="color: #339933;">=</span> Cheese.<span style="color: #000000; font-weight: bold;">class</span>, hint <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;parmesan&quot;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ParmesanCheese
    <span style="color: #000000; font-weight: bold;">implements</span> Cheese
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> slice<span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> slices <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;No can do&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getAroma<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;strong&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>All you need to do is add the <code>@Component</code> annotation, to define the role (the interface name from above) and a hint (to identify a unique implementation).</p>

<h3>Creating a Component Descriptor</h3>

<p>Other then telling you how to create it, and what it is for, I am not going to tell you much about it.  This file will be generated for you in <code>META-INF/plexus/components.xml</code> inside your jar.</p>

<p>The component descriptor describes the component and its dependencies (we will get to that next).  All you need to do is configure the plexus-component-metadata plugin in your pom.  And of course you need to add your maven dependencies.</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- The Plexus annotations --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.codehaus.plexus<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>plexus-component-annotations<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0-beta-3.0.5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- The plexus container --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.codehaus.plexus<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>plexus-container-default<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0-beta-3.0.5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
      <span style="color: #808080; font-style: italic;">&lt;!-- Needed to process the annotations to create an xml file --&gt;</span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.codehaus.plexus<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>plexus-component-metadata<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0-beta-3.0.5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>process-classes<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>generate-metadata<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>process-test-classes<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>generate-test-metadata<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<h3>Injecting Dependencies</h3>

<p>The above is all great and stuff but so far we haven&#8217;t injected anything yet.</p>

<p>First we will add another Cheese.  This will be the default implementation.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">example</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.codehaus.plexus.component.annotations.Component</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.codehaus.plexus.component.annotations.Requirement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.codehaus.plexus.logging.Logger</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * AmericanCheese is the default cheese in most places. So here we are using it as the default component. Note: no
 * &lt;code&gt;hint&lt;/code&gt; is specified.
 */</span>
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span> role <span style="color: #339933;">=</span> Cheese.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AmericanCheese
    <span style="color: #000000; font-weight: bold;">implements</span> Cheese
<span style="color: #009900;">&#123;</span>
    @Requirement
    Logger logger<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> slice<span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> slices <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">logger</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Slicing the cheese: &quot;</span> <span style="color: #339933;">+</span> slices <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; times.&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getAroma<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;plastic&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>Then we will create another interface/component.  Something simple:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">example</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Plate
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">void</span> printItems<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>And now for the fun!</p>

<p>Create a Plate component</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">example</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CheesePlate
    <span style="color: #000000; font-weight: bold;">implements</span> Plate
<span style="color: #009900;">&#123;</span>
...
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> printItems<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    ...
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>To inject the default component, you just add the <code>@Requirement</code> to you field.  Plexus assumes the role is the type of the field.  In this case the field is a <code>Cheese</code></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>17
18
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  @Requirement
  <span style="color: #000000; font-weight: bold;">private</span> Cheese americanCheese<span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>This does the same as the above snippet, but is more verbose.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>23
24
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  @Requirement<span style="color: #009900;">&#40;</span> hint <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;default&quot;</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> Cheese moreAmericanCheese<span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>To inject a specific implementation use the component&#096;s <code>hint</code>.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>29
30
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  @Requirement<span style="color: #009900;">&#40;</span> hint <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;parmesan&quot;</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> Cheese parmesanCheese<span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>To inject all the implementations of a component, just define the <code>role</code>.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>35
36
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  @Requirement<span style="color: #009900;">&#40;</span> role <span style="color: #339933;">=</span> Cheese.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>cheese<span style="color: #339933;">&gt;</span> cheeses<span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>You can also inject the components as a Map, the key of the entry is the component&#096;s <code>hint</code>.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>41
42
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  @Requirement<span style="color: #009900;">&#40;</span> role <span style="color: #339933;">=</span> Cheese.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> Map<span style="color: #339933;">&lt;</span>string, Cheese<span style="color: #339933;">&gt;</span> cheeseMap<span style="color: #339933;">;</span></pre></td></tr></table></div>


<h3>Put Everything Together</h3>

<p>The following class demonstrates the different dependency techniques.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">example</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map.Entry</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.codehaus.plexus.component.annotations.Component</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.codehaus.plexus.component.annotations.Requirement</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span> role <span style="color: #339933;">=</span> Plate.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CheesePlate
    <span style="color: #000000; font-weight: bold;">implements</span> Plate
<span style="color: #009900;">&#123;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Inject the default cheese.
     */</span>
    @Requirement
    <span style="color: #000000; font-weight: bold;">private</span> Cheese americanCheese<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Inject the default cheese, this does the same as the previous line.
     */</span>
    @Requirement<span style="color: #009900;">&#40;</span> hint <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;default&quot;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Cheese moreAmericanCheese<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Inject the ParmesanCheese.
     */</span>
    @Requirement<span style="color: #009900;">&#40;</span> hint <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;parmesan&quot;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Cheese parmesanCheese<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Inject all the cheese.  The container automatically add all the components defined with the role &lt;code&gt;Cheese.class&lt;/code&gt;.
     */</span>
    @Requirement<span style="color: #009900;">&#40;</span> role <span style="color: #339933;">=</span> Cheese.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>cheese<span style="color: #339933;">&gt;</span> cheeses<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * You can also inject the components as a Map, where the key is the Components &lt;code&gt;hint&lt;/code&gt;.
     */</span>
    @Requirement<span style="color: #009900;">&#40;</span> role <span style="color: #339933;">=</span> Cheese.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Map<span style="color: #339933;">&lt;</span>string, Cheese<span style="color: #339933;">&gt;</span> cheeseMap<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> printItems<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;americanCheese smells like: &quot;</span> <span style="color: #339933;">+</span> americanCheese.<span style="color: #006633;">getAroma</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;moreAmericanCheese smells like: &quot;</span> <span style="color: #339933;">+</span> moreAmericanCheese.<span style="color: #006633;">getAroma</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;parmesanCheese smells : &quot;</span> <span style="color: #339933;">+</span> parmesanCheese.<span style="color: #006633;">getAroma</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>The List of cheeses has:&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> Cheese cheese <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">cheeses</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;cheese: &quot;</span> <span style="color: #339933;">+</span> cheese.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>The Map contains:&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> Entry<span style="color: #339933;">&lt;</span>string, Cheese<span style="color: #339933;">&gt;</span> entry <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">cheeseMap</span>.<span style="color: #006633;">entrySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;hint: &quot;</span> <span style="color: #339933;">+</span> entry.<span style="color: #006633;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, value: &quot;</span> <span style="color: #339933;">+</span> entry.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<h3>Executing the Plexus Application</h3>

<p>The final step is to execute the application that uses this component. In this example, you will use an container from a standard Java class with a main() method.</p>

<h4>Creating the container</h4>

<p>Creating the container is very simple:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">example</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.codehaus.plexus.DefaultPlexusContainer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.codehaus.plexus.PlexusContainer</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Example
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args <span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// create a new container</span>
        PlexusContainer container <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultPlexusContainer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        ...
&nbsp;
        <span style="color: #666666; font-style: italic;">// stop the components and container</span>
        container.<span style="color: #006633;">dispose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>That&#8217;s all there is to it: create the container, and start it. Defaults and the current classloader will be used.</p>

<h4>Retrieving the Component</h4>

<p>To retrieve the Plate component from the container and execute it&#8217;s printItems() method, add the following lines after those that start the container:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>14
15
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  Plate plate <span style="color: #339933;">=</span> container.<span style="color: #006633;">lookup</span><span style="color: #009900;">&#40;</span> Plate.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  plate.<span style="color: #006633;">printItems</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>That&#8217;s all there is to getting started with Plexus. Congratulations!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2009/05/plexus-container-five-minute-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
