Java Annotations with JRuby: A Spring MVC Example

To illustrate the use of Java annotations with JRuby code, I have put together a little Spring MVC example. This is quite straightforward, especially if you are familiar with Spring MVC already.

The Controller in JRuby

The “heart” of the app is the Spring MVC controller which must be annotated with the Controller annotation, and its request path defined with the RequestMapping annotation:

We also put the class in the com.weblogism.myapp package, we’ll see why in a second.

Spring Configuration

The Spring MVC configuration is pretty much “standard”:

It defines the component-scan tag that will look for all the classes annotated with Controller in the com.weblogism.myapp; you now see why we used java_package for our controller.


The compiling and packaging is done by Maven. Again, nothing really extraordinary in the pom.xml. The only unconventional feature is the use of the jruby-maven-plugin to compile our JRuby class into a Java class:

It generates its output into target/generate-sources/jruby, and compiles out class into target/classes, like any other Java class. The output of the build is a war file that can be deployed in a Java EE container.


To see our amazing app in action, run Jetty:

 mvn jetty:run

Once Jetty is up, you can access the app at http://localhost:8080/welcome.html



  1. Thank you for this excellent, concise and very useful article.

    Do you know if this can work in JIT mode too? (I mean dynamically creating the class within a host application, without compilation)

    Gergo · 2013-06-07 00:43 · #