I'm trying to set up the Spring 1.0.1 module to work with Play! 1.2.1.
Firstly I added the Spring module to the dependencies.yml:
- play -> spring 1.0.1
Then I added the following as per the documentation in the application.conf:
play.spring.component-scan=true
Then I ran the following to download the dependencies and update my IntelliJ configuration:
play deps --sync
play idealize
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 spring-1.0.1/lib directory. If I try and annotate one of my classes with @Component, it cannot find it. However, if I manually add spring-1.0.1/lib 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?
The second part of my question relates to wiring up a simple example and writing a test for it.
So let's say I have a Job class like this:
@Component
@Every("10min")
public class MyJob extends Job {
private @Inject Printer printer;
@Override
public void doJob() throws Exception {
printer.print();
}
}
And the Printer class looks like this:
@Component
public class Printer {
public void print() {
System.out.println("foo");
}
}
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"?
Update: As mentioned by Aaron, I changed the @Inject to be @Autowired. In my test class, I have something like this:
@Test
public void testSomething() {
MyJob job = play.modules.spring.Spring.getBeanOfType(MyJob.class);
job.doJob();
...
}
It fails on that getBeanOfType statement with the following:
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)
Update 2: 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).
Now I just need to sort out how to inject a different version of the Printer object that spits out "bar" instead of "foo".
Update 3: 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:
@Component("printer")
public class RealPrinter implements Printer {
public void print() {
System.out.println("foo");
}
}
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.
@Component("printer")
public class TestPrinter implements Printer {
public void print() {
System.out.println("bar");
}
}
However, because I have two components qualified with the name "printer", Play's Spring Module throws the following exception:
play.exceptions.UnexpectedException: Unexpected Error
at play.Play.start(Play.java:491)
at play.test.PlayJUnitRunner.<init>(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
As you can see, the cause of the exception is that there are now two Printers with the same name.