title: Writing a simple Spring example using the Play! Framework
body: <p>I'm trying to set up the <b>Spring 1.0.1 module</b> to work with <b>Play! 1.2.1</b>.</p>
  
  <p>Firstly I added the Spring module to the dependencies.yml:</p>
  
  <pre><code>- play -&gt; spring 1.0.1
  </code></pre>
  
  <p>Then I added the following as per the <a href="http://www.playframework.org/modules/spring-1.0.1/home" rel="nofollow">documentation</a> in the application.conf:</p>
  
  <pre><code>play.spring.component-scan=true
  </code></pre>
  
  <p>Then I ran the following to download the dependencies and update my IntelliJ configuration:</p>
  
  <pre><code>play deps --sync
  play idealize
  </code></pre>
  
  <p>Now when I go back to IntelliJ, I can see that spring-1.0.1 has been added to my list of project modules which looks correct.  However, if I look at the dependencies for my project it doesn't have the jars located in the <b>spring-1.0.1/lib</b> directory.  If I try and annotate one of my classes with @Component, it cannot find it.  However, if I manually add <b>spring-1.0.1/lib</b> as a project dependency it finds @Component just fine.  Am I suppose to run something else to get my IntelliJ project to recognize the spring libraries found in the module?</p>
  
  <p>The second part of my question relates to wiring up a simple example and writing a test for it.</p>
  
  <p>So let's say I have a Job class like this:</p>
  
  <pre><code>@Component
  @Every("10min")
  public class MyJob extends Job {
      private @Inject Printer printer;
  
      @Override
      public void doJob() throws Exception {
          printer.print();
      }
  }
  </code></pre>
  
  <p>And the Printer class looks like this:</p>
  
  <pre><code>@Component
  public class Printer {
      public void print() {
          System.out.println("foo");
      }
  }
  </code></pre>
  
  <p>Firstly, does the annotating look correct?  And secondly, how would I go about writing a test that would call MyJob.doJob() but inject a different Printer that would print "bar" instead of "foo"?  </p>
  
  <p><b>Update:</b> As mentioned by Aaron, I changed the @Inject to be @Autowired.  In my test class, I have something like this:</p>
  
  <pre><code>@Test
  public void testSomething() {
      MyJob job = play.modules.spring.Spring.getBeanOfType(MyJob.class);
      job.doJob();
      ...
  }
  </code></pre>
  
  <p>It fails on that getBeanOfType statement with the following:</p>
  
  <pre><code>play.modules.spring.SpringException
  at play.modules.spring.Spring.getBean(Spring.java:11)
  at com.testing.JobTest.testSomething(MyJob.java:20)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
  at play.test.PlayJUnitRunner$StartPlay$1$1$1.execute(PlayJUnitRunner.java:73)
  at play.Invoker$Invocation.run(Invoker.java:265)
  at play.Invoker.invokeInThread(Invoker.java:67)
  at play.test.PlayJUnitRunner$StartPlay$1$1.evaluate(PlayJUnitRunner.java:68)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
  at play.test.PlayJUnitRunner.run(PlayJUnitRunner.java:48)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:60)
  </code></pre>
  
  <p><b>Update 2: </b>Okay I've actually gotten further now.  It seems as though even though I am doing component scanning, the Spring module still requires an application-context.xml in the conf/ directory (even though it defines no beans). </p>
  
  <p>Now I just need to sort out how to inject a different version of the Printer object that spits out "bar" instead of "foo".</p>
  
  <p><b>Update 3: </b>I have replaced using @Autowired with @Resource(name="printer") and I have annotated my production printer with @Component("printer") and renamed it to RealPrinter and provided an interface:</p>
  
  <pre><code>@Component("printer")
  public class RealPrinter implements Printer {
      public void print() {
          System.out.println("foo");
      }
  }
  </code></pre>
  
  <p>If I have just the above Printer, the injection works fine.  Now if I create a TestPrinter, I need Play's Spring Module to realize that I want to use the TestPrinter and not the RealPrinter when I'm executing a test class.</p>
  
  <pre><code>@Component("printer")
  public class TestPrinter implements Printer {
      public void print() {
          System.out.println("bar");
      }
  }
  </code></pre>
  
  <p>However, because I have two components qualified with the name "printer", Play's Spring Module throws the following exception:</p>
  
  <pre><code>play.exceptions.UnexpectedException: Unexpected Error
  at play.Play.start(Play.java:491)
  at play.test.PlayJUnitRunner.&lt;init&gt;(PlayJUnitRunner.java:31)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
  at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
  at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
  at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
  at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
  at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
  at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:66)
  at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:192)
  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:60)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)
  Caused by: java.lang.IllegalStateException: Annotation-specified bean name 'printer' for bean class [com.testing.job.RealPrinter] conflicts with existing, non-compatible bean definition of same name and class [com.testing.job.TestPrinter]
  at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:267)
  at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:208)
  at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.scan(ClassPathBeanDefinitionScanner.java:180)
  at play.modules.spring.SpringPlugin.onApplicationStart(SpringPlugin.java:98)
  at play.plugins.PluginCollection.onApplicationStart(PluginCollection.java:408)
  at play.Play.start(Play.java:463)
  ... 19 more
  </code></pre>
  
  <p>As you can see, the cause of the exception is that there are now two Printers with the same name.</p>