首先强烈遣责water ye同学不负责任的行为,那家伙在blog上贴了
一段spring 2.0m2的test代码就了事,居然不测试一下,结果俺的spring groovy test代码折腾半天才过去。
测试代码俺就不贴了,spring的配置就够了
1 <?xml version="1.0" encoding="GBK"?>
2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
3 "http://www.springframework.org/dtd/spring-beans.dtd">
4 <beans>
5 <bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor" />
6 <bean id="inlineMessenger" class="org.springframework.scripting.groovy.GroovyScriptFactory">
7 <constructor-arg>
8 <value>inline:
9 package spring.groovy;
10
11 class GroovyInlineMessenger implements Messenger{
12 void sayHello(){
13 System.err.println("Hello");
14 }
15 }
16 </value>
17 </constructor-arg>
18 </bean>
19 <bean id="messenger" class="org.springframework.scripting.groovy.GroovyScriptFactory">
20 <constructor-arg value="classpath:spring/groovy/GroovyMessenger.groovy" />
21 <property name="message" value="Hello World!" />
22 </bean>
23 </beans>
1、第5行的bean定义一定要有,估计是提前编译groovy脚本的(具体的可能啃一下源码就明白了)。
2、inlineMessenger这个bean的定义,inline一定要紧跟着<value>写,我为了美观,弄成下面两种写法都没过去。
1 <bean id="inlineMessenger" class="org.springframework.scripting.groovy.GroovyScriptFactory">
2 <constructor-arg>
3 <value>
4 inline:
5 package spring.groovy;
6
7 class GroovyInlineMessenger implements Messenger{
8 void sayHello(){
9 System.err.println("Hello");
10 }
11 }
12 </value>
13 </constructor-arg>
14 </bean>
1 <bean id="inlineMessenger" class="org.springframework.scripting.groovy.GroovyScriptFactory">
2 <constructor-arg>
3 <value>
4 <![CDATA[inline:
5 package spring.groovy;
6
7 class GroovyInlineMessenger implements Messenger{
8 void sayHello(){
9 System.err.println("Hello");
10 }
11 }]]>
12 </value>
13 </constructor-arg>
14 </bean>
写成这样倒是过的
1 <bean id="inlineMessenger" class="org.springframework.scripting.groovy.GroovyScriptFactory">
2 <constructor-arg>
3 <value><![CDATA[inline:
4 package spring.groovy;
5
6 class GroovyInlineMessenger implements Messenger{
7 void sayHello(){
8 System.err.println("Hello");
9 }
10 }]]>
11 </value>
12 </constructor-arg>
13 </bean>
俺平时习惯让myeclipse对xml进行格式化,照这么看来,估计一不小心,按个ctrl+shift+F,好端端的spring groovy代码就过不去了,然后就慢慢找问题吧。