Showing posts with label Groovy. Show all posts
Showing posts with label Groovy. Show all posts

Spring Example: Wired Together


package packageA;

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

public class HelloApp2 
{
   public static void main(String[] args) 
   {
     ApplicationContext ctx = new ClassPathXmlApplicationContext("config.xml");
     GreetingService greetingService = (GreetingService) ctx.getBean("greetingService");
     greetingService.sayALotOfGreeting();
     greetingService.theWinnderOfOscar();
     greetingService.sayGreeting();

     Lime iLime = (Lime) ctx.getBean("lime");
     iLime.drink();
   }
}

Spring Example: Groovy File


class Lime implements packageA.Lime 
{
    void drink() 
    {
      print "Groovy not in line!"
    }
}

Spring Example: Config file

Groovy in Spring

Now I could inject Groovy class into Spring, but the problem is Groovy class has to implement a Java interface, in this way, I could get the bean from application context.

I am wondering is it possible that I could get Groovy class without using interface?

Groovy in Spring

--------------------for DI---------------------
org.springframework.asm-3.1.0.M1.jar
org.springframework.beans-3.1.0.M1.jar
org.springframework.context-3.1.0.M1.jar
org.springframework.core-3.1.0.M1.jar
org.springframework.expression-3.1.0.M1
c:\springsource\spring-framework-3.1.0.M1\dist\dependencies\commons-logging-1.1.1.jar
--------------------for method injection---------------------
c:\springsource\spring-framework-3.1.0.M1\dist\dependencies\cglib-nodep-2.2.jar
--------------------for Groovy integration------------------
c:\springsource\spring-framework-3.1.0.M1\dist\dependencies\aopalliance.jar
org.springframework.aop-3.1.0.M1.jar
c:\groovy-1.7.2\lib\antlr-2.7.7.jar
c:\groovy-1.7.2\lib\asm-3.2.jar
c:\groovy-1.7.2\lib\groovy-1.7.2.jar

Groovy

groovy: An agile dynamic language for the Java Platform.

A quick introduction:


Installation:
1. It's zip file, just unzip it to some directory. For example: C:\groovy-1.7.2.
2. Go to C:\groovy-1.7.2\bin, execute groovyConsole.bat (windows) or groovyConsole (Unix).
3. Now you have a groovy console. Now this is what I need, I really don't have much interest to learn some new grammar, but GroovyConsole recognize the normal Java codes too, so I would like to use GroovyConsole as a Java Console. In this way, I could execute some simple Java codes directly in console.
4. How to add some existing library? Script->Add Jar to classpath or Add Directory to classpath.

For example, if I want to test some methods for StringUtils, I just need download the jar, and add it to classpath. Write some codes in Script console:

import org.apache.commons.lang.StringUtils;

String str = StringUtils.leftPad("999", 5, 'x');
System.out.println(str);

*****

Basically just treat the console as a Java file, the nice thing about it is you don't need to compile the codes.

Strange thing: seem Groovy Console doesn't like String[] stars = {"Tom", "Sam"};

But it's ok to use String[] stars = ["Tom", "Sam"];

System.out.println == println

println "${new Date()}"

****************************
class HelloWorld
{
def name
def greet() { "Hello ${name}" }
}

def helloWorld = new HelloWorld()

helloWorld.name = "Groovy"

println helloWorld.greet()