devxlogo

Define and Execute a Groovy Bean Inside a Spring Application Context

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:

                    

Finally, here’s an execution example:

HelloWorldService service = (HelloWorldService) context.getBean(   "helloWorldService");   System.out.println(service.sayHello());

devx-admin

Share the Post: