Thursday, August 2, 2012

Spring Groovy Inline Scripting Extended


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

public interface Drinker {
      public String getDrink();

      public void setDrink(String drink);
}
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[
      public class MyDrinker implements com.lnn.groovy.Drinker {
        public String getDrink() {
          return drink;
        }
        public void setDrink(String drink){
            this.drink = drink;
        }
        private String drink;
      }
      ]]>
            </lang:inline-script>
            <lang:property name="drink" value="Drink 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");
            drinker.setDrink("Drink From Java.");
            System.out.println(drinker.getDrink());
      }
}

No comments:

Post a Comment