Calling Spring Beans from JRuby on Rails app

Hi,

I have a JRuby on Rails application which needs to invoke a Spring Bean.

----- prod_defs_controller.rb -----
require ‘java’
require ‘path_to_jars/some.jar’

module XXX
import com.xxx.SpringBeanName
end

class ProdDefsController < ApplicationController
def index
java_import java.lang.System
@jvm_version = System.getProperties[“java.runtime.version”]
@screenData = XXX::SpringBeanName.getData(“param”)
end

I am getting NoMethodError. See below.
(1) Is there a less painful way to invoke spring beans from jruby?

(2) Are there any books or blog posts on integrating JRoR frontend with
Spring based backend?

------ Glassfish console output ----------
INFO: Application XXX undeployed
INFO: Running XXX in development environment.
INFO: Rails application detected.
INFO: Loading application XXX at /XXX
INFO: JRuby version is: 1.4.0
INFO: XXX was successfully deployed in 1,058 milliseconds.
INFO: New instance of JRuby runtime created in 3,064 milliseconds
SEVERE: JRuby limited openssl loaded. gem install jruby-openssl for full
support.
http://jruby.kenai.com/pages/JRuby_Builtin_OpenSSL
INFO: New instance of rails created in 9,837 milliseconds
INFO: Pool started without dynamic resizing enabled. Pool will not
attempt to determine the upper and lower bounds that it should be using,
and will stay at 1
INFO: SQL (10.0ms) SET SQL_AUTO_IS_NULL=0
INFO: Processing ProdDefsController#index (for 0:0:0:0:0:0:0:1%0 at
2010-03-13 20:13:32) [GET]
INFO: NoMethodError (undefined method getScreenData' for Java::ComXxx::SpringBeanName:Class): app/controllers/prod_defs_controller.rb:20:in index’
file:/Applications/glassfishv3/glassfish/modules/grizzly-jruby.jar!/rack/adapter/rails.rb:133:in
call' file:/Applications/glassfishv3/glassfish/modules/grizzly-jruby.jar!/rack/handler/grizzly.rb:55:in call’
:1
INFO: Rendered rescues/_trace (335.0ms)
INFO: Rendered rescues/_request_and_response (48.0ms)
INFO: Rendering rescues/layout (internal_server_error)
INFO: SQL (1.0ms) SET SQL_AUTO_IS_NULL=0
INFO: Processing ProdDefsController#index (for 0:0:0:0:0:0:0:1%0 at
2010-03-13 20:13:33) [GET]
INFO: NoMethodError (undefined method getData' for Java::ComXxx::SpringBeanName:Class): app/controllers/prod_defs_controller.rb:20:in index’
file:/Applications/glassfishv3/glassfish/modules/grizzly-jruby.jar!/rack/adapter/rails.rb:133:in
call' file:/Applications/glassfishv3/glassfish/modules/grizzly-jruby.jar!/rack/handler/grizzly.rb:55:in call’
:1
INFO: Rendered rescues/_trace (181.0ms)
INFO: Rendered rescues/_request_and_response (14.0ms)
INFO: Rendering rescues/layout (internal_server_error)


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi Vineet,

In my case Spring beans where described in an applicationContext.xml
file. I had to create a Spring context to call the beans.
Therefore I wrote an initalizer (in /config/initializers/01_spring.rb)

SPRING_XML_CONFIG_FILES =
%w(applicationContext).map { |c|
“/#{RAILS_ROOT}/config/spring/#{c}.xml”
}.to_java :string
SPRING_CONTEXT =
org.springframework.context.support.FileSystemXmlApplicationContext.new(SPRING_XML_CONFIG_FILES)

In my controllers it is now possible to reference the beans:
SPRING_CONTEXT.get_bean(“myBean”)
In my case I included a web app which uses Spring, I don’t know if this
fits four you, maybe it brings you some steps further.

kind regards,
marco

Am 14.03.2010 16:15, schrieb Vineet Bhatia:

end

SEVERE: JRuby limited openssl loaded. gem install jruby-openssl for full support.
INFO: Rendered rescues/_trace (335.0ms)
INFO: Rendered rescues/_request_and_response (14.0ms)


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Thanks Marco. I was able to get the spring config loaded.
But now I am getting ClassNotFoundException from Spring framework.
I combined all jar files into a single jar. This jar includes the spring
bean I want to use from jruby. I also have “require” in the spring
initializer and ruby controller.

require ‘/path_to/plusApp/lib/all.jar’

---- Glassfish console ------------
INFO: Refreshing
org.springframework.context.support.FileSystemXmlApplicationContext@6b3755b5:
display name
[org.springframework.context.support.FileSystemXmlApplicationContext@6b3755b5];
startup date [Sun Mar 14 15:02:40 EDT 2010]; root of context hierarchy
INFO: Loading XML bean definitions from file
[/Users/vineetb/vineetb_dev/Learn/ror/plusApp/config/applicationContext.xml]
INFO: Bean factory for application context
[org.springframework.context.support.FileSystemXmlApplicationContext@6b3755b5]:
org.springframework.beans.factory.support.DefaultListableBeanFactory@c266fa0
INFO: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@c266fa0:
defining beans [MlScreenDataServiceBroker,ProducerSearchServiceBroker];
root of factory hierarchy
INFO: Destroying singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@c266fa0:
defining beans [MlScreenDataServiceBroker,ProducerSearchServiceBroker];
root of factory hierarchy
SEVERE: org.springframework.beans.factory.CannotLoadBeanClassException:
Cannot find class [com.hanover.cl.plus.broker.MlScreenDataServiceBroker]
for bean with name ‘MlScreenDataServiceBroker’ defined in file
[/Users/vineetb/vineetb_dev/Learn/ror/plusApp/config/applicationContext.xml];
nested exception is java.lang.ClassNotFoundException:
com.hanover.cl.plus.broker.MlScreenDataServiceBroker
from
org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java:524:in
predictBeanType' from org/springframework/beans/factory/support/AbstractBeanFactory.java:1174:in isFactoryBean’

On Mar 14, 2010, at 1:18 PM, Marco D. wrote:

java_import java.lang.System

INFO: Application XXX undeployed
INFO: SQL (10.0ms) SET SQL_AUTO_IS_NULL=0
INFO: Processing ProdDefsController#index (for 0:0:0:0:0:0:0:1%0 at 2010-03-13 20:13:33) [GET]

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi Vineet,

some months ago I wrote a rails plugin to load spring beans into RoR
applications in an easy way, I never published because I never tested it
but
I think it’s almost complete and perhaps it can help you:

Hi,

are you sure that the class is located in your jar file?
Are you working with an application server? I use glassfish for my apps
and I deploy my JRoR app in form of a war file which I create with
warbler. How do you deploy?

What you also can do is to check whether the class can be called
directly. I used script/console, required the according jar, imported
the class and created an object. This way shows you where jruby can’t
load something.

kind regards,
marco

Vineet Bhatia schrieb:

INFO: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@6b3755b5]: org.springframework.beans.factory.support.DefaultListableBeanFactory@c266fa0

In my case I included a web app which uses Spring, I don’t know if this fits four you, maybe it brings you some steps further.

----- prod_defs_controller.rb -----
@jvm_version = System.getProperties[“java.runtime.version”]
INFO: Running XXX in development environment.
INFO: Processing ProdDefsController#index (for 0:0:0:0:0:0:0:1%0 at 2010-03-13 20:13:32) [GET]
INFO: NoMethodError (undefined method `getData’ for Java::ComXxx::SpringBeanName:Class):
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email