Thursday, August 2, 2012

Spring Groovy Inline Scripting


Following is the implementation of groovy inline scripting in spring.
Implementation:
Drinker:
package com.lnn.groovy;

public interface Drinker {
      public String getDrink();
}
Groovy.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/lang
      http://www.springframework.org/schema/lang/spring-lang-3.0.xsd">
      <lang:groovy id="drink">
            <lang:inline-script><![CDATA[
      class MyDrinker implements com.lnn.groovy.Drinker {
        String getDrink() {
          return value;
        }
        String value;
      }
      ]]>
            </lang:inline-script>
            <lang:property name="value" value="Drinking from Groovy." />
      </lang:groovy>
</beans>
GroovyTest.java:
package com.lnn.groovy;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class GroovyTest {
      public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("groovy.xml");
            Drinker drinker = (Drinker) context.getBean("drink");
            System.out.println(drinker.getDrink());
      }
}

No comments:

Post a Comment