Ruby On Spring

At my company, we are currently developing an open source package called Ruby on Spring that integrates the JRuby dynamic language with the Spring application framework. Ruby on Spring is open source and licensed under the MIT license
The goals of the project are to permit rapid innovation and iteration, full reuse of our existing Spring and Hibernate business logic and objects, and a smooth transition from prototype to production code.
We were inspired by many of the benefits and philosophy of Ruby on Rails, including avoiding configuration files and compilation. Make a change, and you’ll see it immediately. At the same time, if you’re already commited to Spring, the package doesn’t require you to learn a new framework or abandon your existing investment in Spring. The total size of the package is less than a 1000 lines, so it’s small and easily integrated into existing projects.
The key part of the system is a Spring controller that instantiates a JRuby script engine and executes a JRuby script to define the controller and the model. The view is provided by a standard JSP file. If the JRuby controller is replaced by a Java one, the view can be used as-is with no changes.
Download
You can download a zip file include the source code, a precompiled JAR, and installation instructions from rubyonspring.zip. We have tested the JAR against stock distributions of Spring, Hibernate, and Tomcat; a list of required libraries is included in the installation instructions.
| Version History | ||
| 0.1 | 11/28/05 | Initial Release |
Tutorial
Hello World
The simplest possible example consists of a JRuby script “hello.rb” which returns a model object and a JSP view “hello.jsp” which uses that model. To try the example, I just need to drop these two files into the “rad” directory on my Tomcat server. No compilation, configuration files, or server restarts are required.
{ ‘greeting’ => ‘Hello world’}
<html>
${command[’greeting’]}
</html>
Notice that rather than defining an explicit controller class, the script simply returns a model object. This is a shortcut for rapid prototyping. Equivilantly, we could have explicitly defined a controller object as follows:
class DemoController
def getModel(request)
{ ‘greeting’ => ‘Hello world’}
end
end
DemoController.new()
Reusing Spring Beans
Here’s a more interesting example that reuses business logic from a Spring bean implemented in Java. ($context represents the Spring application context.) Thanks to the magic of JRuby, we can interact and create Java objects just as easily as Ruby ones.
locationCalculator = $context.getBean(’locationCalculator’)
{’locations’ => locationCalculator.getPointMatches(’seattle’)}
Hibernate Queries
The package also supports Hibernate. The follow example uses Hibernate to fetch a list of company objects starting with a given letter. Note that the query returns full fledged Java objects with behavior, not just data.
s = $request.getParameter(’s’)
companies = $helper.queryHibernate(”from jobster.model.Company where name>=?”, [s])
{’companies’ => companies, ‘label’ => “Companies starting with #{s}”}
The corresponding JSP view uses standard Spring conventions and could be used without changes with a Java controller.
<div class=”left pad”>
<div class=”bold”>Companies: ${command.label}</div>
<ol>
<c:forEach var=”company” items=”${command.companies}”>
<li> ${company.name} <br />
</li></c:forEach>
</ol>
</div>
onSubmit Handlers
Our final example is a simple calculator controller. It implements an onSubmit handler to run business logic (computing the calculation) when the user submits the form and choose a new result view.
class CalculatorController
def getModel(request)
{’x’ => 2, ‘y’ => 2}
end
def onSubmit(request, response, form, errors)
form[’total’] = form[’x'].to_i + form[’y'].to_i
form[’viewName’] = ‘/rad/calculator_results’
return form
end
end
CalculatorController.new()
What’s next
There are a number of items still to be completed, but already the framework has proven useful for several rapid prototyping projects that subsequently became product features.
We need to fully support the <spring :bind> tag or something like it. We’d like to define additional helpers to simplify Hibernate queries, in the style of Rail. More complete documentation and locked down interfaces are on the roadmap.
If you have any issues, or are interested in collaborating, please contact me or Laurel Fan.
7 Comments so far
Leave a comment
[…] The Ruby on Spring project page now includes a general download link for those wishing to try the package. […]
By Bogle’s Blog » Ruby on Spring now available for download on 11.28.05 3:48 pm
Hello,
This is a very simple and powerful tool you have given the community. This also looks like a nice starting point for having a complete EAI environment. This would help to put up a lean competitor to some of the existing and cubersome java ones. Thank you. Good day.
Yemi Bedu
By Yemi Bedu on 11.29.05 11:12 am
[…] .ruby {margin-top: 1em; background-color:#F0F0F0; border-color: silver; font-family: courier; font-size: x-small} .jsp {margin-top: 1em; background-color:#F0F0E0; border-color: silver; font-family: courier; font-size: x-small} .title{color:#F0F0E0; background-color: #AAA; border-color: silver;} Download < a href=”http://thebogles.com/blog/projects/ruby-on-spring/”>Download Ruby on Spring here At Jobster, we are currently developing a rapid prototyping package that integrates the JRuby dynamic language with the Spring application framework. This post provides an introduction via examples. The goals of the project are to permit rapid innovation and iteration, full reuse of our existing Spring and Hibernate business logic and objects, and a smooth transition from prototype to production code. We were inspired by many of the benefits and philosophy of Ruby on Rails, including avoiding configuration files and compilation. Make a change, and you’ll see it immediately. At the same time, if you’re already commited to Spring, the package doesn’t require you to learn a new framework or abandon your existing investment in Spring. The total size of the package is less than a 1000 lines, so it’s small and easily integrated into existing projects. The key part of the system is a Spring controller that instantiates a JRuby script engine and executes a JRuby script to define the controller and the model. The view is provided by a standard JSP file. If the JRuby controller is replaced by a Java one, the view can be used as-is with no changes. […]
By Bogle’s Blog » Ruby On Spring on 04.03.06 9:08 am
[…] 项目主页:http://thebogles.com/blog/projects/ruby-on-spring/ […]
By 秀板儿 » Blog Archive » Ruby on Spring on 07.20.08 9:23 am
[…] Ruby on SpringÊÇÒ»¸ö²ÉÓÃRuby on RailsµÄ˼ÏëÓëÓŵ㣬¼¯³ÉJRuby¶¯Ì¬ÓïÑÔÓëSpring¿ò¼Ü¶ø¿ª·¢µÄ¿ªÔ´ÏîÄ¿¡£»ùÓÚMIT license·¢²¼¡£ ¸ÃÏîÄ¿Ö÷Ò³:http://thebogles.com/blog/projects/ruby-on-spring/ […]
By zhebook的博客 » Ruby on Spring on 03.15.09 2:20 am
[…] ¸ÃÏîÄ¿Ö÷Ò³:http://thebogles.com/blog/projects/ruby-on-spring/ ±¾Ò³¡¾Web¿ª·¢¿ò¼Ü¡¿ÆäËü¿ªÔ´ÏîÄ¿ […]
By zhebook的博客 » Ruby on Spring on 03.15.09 2:34 am
[…] ¸ÃÏîÄ¿Ö÷Ò³:http://thebogles.com/blog/projects/ruby-on-spring/ […]
By zhebook的博客 » Ruby on Spring on 03.15.09 2:41 am
Leave a comment