Thursday, June 21, 2007

include Spring

I've gotten a little farther with my JRuby on Rails Spring integration code. I think I have what may make the beginning of a Spring plugin for JRuby on Rails. Here's the code for the Spring module. This is my first foray into metaprogramming in Ruby, I'd welcome any feedback.

# spring.rb
# June 20, 2007
#


module Spring
include Java
import javax.servlet.ServletContext
import org.springframework.web.context.WebApplicationContext
import org.springframework.web.context.support.WebApplicationContextUtils

def spring_bean (bean)
class_eval "def #{bean.to_s}; application_context.get_bean(\"#{bean.to_s}\"); end;"
end

# dunno if there is a better way to do this. I need the spring_bean method
# to become a class method on the includee, but application_context needs to be
# a normal instance method
def self.append_features(includee)
includee.extend(Spring)
includee.class_eval do
def application_context
WebApplicationContextUtils.getWebApplicationContext($servlet_context)
end
end
end
end

This gives you a handy spring_bean method in any class where you've included Spring. The spring_bean method will then dynamically add an accessor to the class you use it in. This is what it looks like in a controller:

class TestSpringController < ApplicationController
include Spring
spring_bean :descriptor1
def showcontext
render_text descriptor1.displayName
end

end


Obviously to have this working you need several things. You need to have the goldspike plugin (from SVN trunk as per my earlier post). You also need to have Spring and its dependent jars in your WEB-INF/lib. I just copied em in for playing around purposes, with a little tweaking of my conf/war.rb file I could probably make goldspike fetch them. And lastly, it assumes you have an applicationContext.xml in WEB-INF, and that you have the Spring ContextLoaderListener declared in your web.xml.

Wow, that's a lot of trouble, why the heck would I want to do all that? What's this all for anyways? First of all, if you need this you probably don't need to ask, you know who you are. No, I'm not even trying to suggest using Spring for greenfield JRuby on Rails applications. But if you need to do heavy Java integration or you have an existing Spring application you want to front end in Rails, this kind of thing could end up being quite handy. This is my situation: I have a pretty large Spring/Hibernate/Tapestry app and I'd like to make moving to JRuby on Rails a viable option. Rather than replace everything whole hog, I'd like to be able to leverage working Spring services easily from Rails. As you can see, doing this should be a piece of cake.

17 comments:

dacc said...

Nice work, and very useful. =)

Thanks for posting the details.

El Raichu said...

i think the recommended way of extending is through self.included base instead of self.append_features base (even though rails uses #append_features instead of #included).

Thomas E said...

You can probably take this a step further and have the first reference to spring bean method redefine itself so it does not need to go through application_context.get_bean() every time. Not sure it would make a big difference, but it is an option.

Adi said...

Find Internet Marketing resource hare Online Marketing Strategy Internet Marketing Tools Online Marketing Campaign Online Marketing Business Online Marketing System Online Business Online Home Business Online Business Tips Internet Marketing Online Online Marketing Affiliate Marketing Online Marketing Success Selling Online Free Online Marketing Online Marketing Tools Online Marketing Blog

Adi said...

Oes Tsetnoc one of the ways in which we can learn seo besides Mengembalikan Jati Diri Bangsa. By participating in the Oes Tsetnoc or Mengembalikan Jati Diri Bangsa we can improve our seo skills. To find more information about Oest Tsetnoc please visit my Oes Tsetnoc pages. And to find more information about Mengembalikan Jati Diri Bangsa please visit my Mengembalikan Jati Diri Bangsa pages. Thank you So much.
Oes Tsetnoc | Semangat Mengembalikan Jati Diri Bangsa

aiya said...

Office 2010is powerful!
Microsoft Office 2010is the best software in the world.
Microsoft wordis so great!
Office 2007makes life great!
Many people likeMicrosoft Office.
Microsoft Office 2007is welcomed by the whole world.
Office 2007 keyis available here.
Office 2007 downloadis on sale now!
Office 2007 Professionalbring me so much convenience.
Outlook 2010 make life wonderful!
Microsoft outlook is my love.
Microsoft outlook 2010 is convenient!
Windows 7 is convenient!

teramoney said...

Tera gold
wow gold

Tera gold
Tera Item
Tera account

buy eden gold
Runescape gold
buy Runescape gold

Adim said...

Wow, excellent data regarding this issue. You might want to let me know how long will it take? Because I like to write the thesis or may be it would be nice to detect the thesis writing service. Thank you very much.


Aerospace Logo

jonalyndislag said...

Ecommerce Australia

I am a businessman so I am able to understand this article very well. I agree with previous comments that indeed this is very informative and can literally help others that are willing to improve. Thank you so much for this post.

Zirong Zhang said...

That is theD3 Items circumstance: I've a fairly significant Spring/Hibernate/Tapestry practical application and also Let me make going to help Buy Runescape GoldJRuby with Side rails a viable option.

akuliop nius said...

Fast delivery + 15% Discount can be assured if you buy buy runescape gold pay by Western Union on Rsorder!

Details at: http://www.rsorder.com/profile/news/read/id/448

Free 200M runescape gold Giveaway on RSorder Flash Sale!

Time: 03:00 AM on December 21, 2012 GMT

To know more: http://www.rsorder.com/profile/news/read/id/441

yolanda zhao said...

The design and style look great though! rs accounts for sale

lisa Rweyer said...

Great article,thank you for you share , i added this url to my bookmark , Look forward to seeing you more post.


Rs gold |Rs 3 Gold |Buy Rs Gold

shorry june said...

Wonderful! cheapest android phones

AntoL-Nyo said...

Ben istanbuldan selda 22 yaşında konfeksiyon içşisiyim Amatör Porno sizleri bekletmeden hemen hikayeme geçiyorum.. Biz 5 katlı bir binanın en üst katında yaşıyorduk ve bir öğlen paydosunda sevgilim beni Anal Sikiş aradı evdeydim çünkü evle iş yerim arasında 2 dakika bile yoktu.. Bende Asyalı Porno sevgilime benden telefon beklemesini söyledikten sonra evin durumuna baktım evde kardeşlerim ve annemin Esmer Porno birkaçtane arkadaşı vardı bu iş imkansızdı tekrardan düşündüm ve çatı katı aklıma geldi sevgilime telefon açıp çatıya çıkmasını ve Fantazi Porno beni beklemesini söyledikten sonra telefonu bir süre elime almadım anneme işe gidiyorum dedikten sonra Gay Porno hemen üst kata çatıya çıkıp sevgilimin
dudaklarına yumuldum az bir süremiz Götten Sikiş vardı ve çok istiyordum onu hemen önünde diz çöküp sikini çıkardım kocaman olmuştu bile... Özlemişti beni HD Porno bitanem başladım sikini yalamaya kafasını öpüyor onu çıldırtıyordum sevgilim saçlarımdan tutmuş ağzımı keyifle sikerken çatı katının kapısı açıldı toparlanamadan Lezbiyen Porno komşunun şişman oğlu efecan gelmişti ben dizlerimin üzerinde sevgilimin Liseli Porno siki ağzımda yakalanmıştım hemen bu halimizi gören komşu oğlu Sarışın Sikiş kaçarcasına gitti.. Sevgilim bir süre güldükten Türbanlı Porno sonra bu şişko neden kaçtı korktu herhalde onu sikicem sandı galiba dedikten sonra bir kahkaha daha attı ben sevgilimin Türk Porno sikine deli gibi saldırıyor yalıyor emiyordum boşalmasını istiyor Zenci Porno sütünü içmek istiyordum sevgilim ağzımı sike sike boşaldı hepsini yuttum bir damlasını ziyan Porno etmedim ve sevgilimle vedalaşıp ayrıldık... Ben her defasında sikiş yapmadan önce yazporno diye olan türkçe siteden Porno izle bölümüne girip seyrediyorum azdıktan sonra şahane sikiş ile gözleri kamaştırıyorum.

Jessica Colin said...

Having read this I believed it was rather informative. I appreciate you finding the time and effort to put this informative article together. I once again find myself spending a lot of time both reading and posting comments. But so what, it was still worth it!

v-travelled |

www.0527musi.com |

axiomconferences.org |

carvinworld |

http://www.customsoftwaremasters.com |

www.findabusinessaddress.com |

im16.co.uk |

mindsehealth |

Moliy Yvonne said...

GOOOOOOOOOOOOOOD Job~~~Thanks for this good article. Excuse me go read it. Hopefully more success. by 4rsgold.com rs gold