Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the way to bootstrap a play 2.4 Java app with Spring?

The documentation at https:// www.playframework.com/documentation/2.4.x/JavaDependencyInjection mentions that DI is now integrated into the framework:

Out of the box, Play provides dependency injection support based on JSR 330. The default JSR 330 implementation that comes with Play is Guice, but other JSR 330 implementations can be plugged in.

I could not find any HOWTO or working example that uses Spring.

The current documentation at https:// www.playframework.com/documentation/2.4.x/Tutorials links to an example "Play with Spring Data JPA" at https:// typesafe.com/activator/template/play-spring-data-jpa but the comments already mention that it's outdated. Also, on the GitHub page https:// github.com/typesafehub/play-spring-data-jpa there are open issues mentioning it's not working in Play 2.4.0.

The code is 2 years old and still uses the Global.java class to bootstrap Spring. I'd be fine with that, but the code does not compile anymore in Play 2.4 because of the method getControllerInstance().

This project https:// github.com/jroper/play-spring looks promising but it's in Scala, and there are many TODOs listed, and the SpringApplicationLoader class at https:// github.com/jroper/play-spring/blob/master/core/src/main/scala/play/api/inject/spring/SpringApplicationLoader.scala looks too complicated when all I want is to start a simple Spring context.

Right now it looks like I have to downgrade Play to use the old Global.java hack (and figure out all the changes needed ...)

(some editor pls put the links back in, they are legit, it would not let me ...)

like image 962
Adrian Pop Avatar asked Jul 21 '15 16:07

Adrian Pop


1 Answers

Multiple people have this problem with Play 2.4, causing some of them to stick with 2.3.

The problem with the text that you quoted from the Play features is that it suggests that with Play 2.4 you can plug in any JSR 330 container, but unfortunately it's very difficult and to date, still no fully working solutions exists. This basically is against Plays own philosophy, which states:

In Play 2, we wanted to make it really easy to use any data store driver, ORM, or any other database access library without any special integration with the web framework. (Which is not the case when you want to use Spring ORM with Play 2.4.)

https://groups.google.com/forum/#!topic/play-framework/hFOtzSNSDsQ also discusses this problem with links to multiple solution attempts, of which the best solution is not fully working, but it may suit your needs: https://github.com/zarinfam/play24-guice-spring

Another solution would be to create a singleton for Springs ApplicationContext and use that singleton within your Play client classes. Then (unfortunately) you don't have dependency injection within those classes, but then at least you can use your non-Play classes in a clean dependency injected way.

Updated: I have just created this working Play-Java-Spring template at GitHub: https://github.com/jtdev/play-java-spring

like image 99
Devabc Avatar answered Sep 21 '22 08:09

Devabc