Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Advanced
Sep 25, 2009

Define and Execute a Groovy Bean Inside a Spring Application Context

A quick example should suffice for defining and executing a Groovy Bean inside a Spring application context. Here's a short Java interface:

package com.springandgroovy;     
public interface HelloWorldService {         
     String sayHello();     
}

And here's the Groovy implementation:

import com.springandgroovy.HelloWorldService;  
class HelloWorldServiceImpl implements HelloWorldService {  
   String name  
   String sayHello()  
   {  
      "Hello $name. Welcome to Scripting in Groovy."  
   }  
 }

The Spring application context is:


 <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-2.5.xsd  
         http://www.springframework.org/schema/lang  
         http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">

 <lang:groovy id="helloWorldService"  
              script-source="classpath:com/springandgroovy/HelloWorldServiceImpl.groovy">  
              <lang:property name="name" value="meera"/>  
 </lang:groovy>

Finally, here's an execution example:

HelloWorldService service = (HelloWorldService) context.getBean(
   "helloWorldService");  
 
System.out.println(service.sayHello());
Santiago Urrizola
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap