5

I have to build an web application that integrate Spring and Hibernate in Play Framework 2.1. I integrated Hibernate successfully, but how can I do it with Spring?

Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
Jack Holmes
  • 99
  • 1
  • 2
  • 10
  • 1
    possible duplicate of [Integrating Play framework 2.0 and Spring framework](http://stackoverflow.com/questions/9703714/integrating-play-framework-2-0-and-spring-framework) – ndeverge Apr 04 '13 at 12:07
  • It's actually a totally different answer in 2.1 compared to 2.0 so this isn't a duplicate. – James Ward Apr 04 '13 at 12:35

3 Answers3

8

It's very easy now in Play 2.1 with the getControllerInstance interceptor. Here is a sample application which has Play 2.1, Spring (with Java Config), and JPA: https://github.com/jamesward/play2bars/tree/java-spring

James Ward
  • 29,283
  • 9
  • 49
  • 85
  • Thanks James. It's very useful for me. – Jack Holmes Apr 08 '13 at 12:01
  • @JackHolmes If you find it useful then why don't you at least upvote the answer and also mark it as the accepted answer. [How do I ask questions here?](http://stackoverflow.com/faq#howtoask) – maba Apr 12 '13 at 06:53
  • @maba: I really wanna vote for this answer but it require 15 reputation to do it. – Jack Holmes Apr 23 '13 at 15:00
  • @JackHolmes I'll give you an upvote and then you'll have all reps needed. Then you should check all your other questions as well and see if they can be upvoted/accepted. – maba Apr 23 '13 at 15:01
0

I recommend the answer from James Ward. Here is some information for upgrading from a Spring Java 2.0 play app to 2.1.

In 2.0.4 I've used the Spring4Play2 dependency, see https://github.com/scott-phillips/Spring4Play2/blob/master/README.markdown but I could not get this to work when upgrading to play 2.1.1 even though there is a 2.1-SNAPSHOT jar of this module at https://maven.library.tamu.edu/content/repositories/snapshots/play/spring_2.9.1/2.1-SNAPSHOT/

The error I got was "Plugin [play.api.db.BoneCPPlugin] cannot been instantiated." and "java.lang.IncompatibleClassChangeError: Found interface play.api.Application, but class was expected" and googling did not bring any answer.

So I went ahead and looked at the nice example from James Ward, dropped that plugin, and just added

"org.springframework" % "spring-context" % "3.2.1.RELEASE",

to my Build.scala as you can see here https://github.com/jamesward/play2bars/blob/java-spring/project/Build.scala

then created the Global.java class similar to the example, and that was about it.

I've added this answer for people running into the same situation so that googling for the error message could bring up a solution.

Gonfi den Tschal
  • 1,754
  • 1
  • 20
  • 29
0

I integrated spring (with Hibernate) within Play 2, you can see an example (full working source) and explanation here: http://serverbabyblog.wordpress.com/2014/03/26/activiti-on-play-2-with-spring-and-hibernate-jpa-example/

Just skip all stuff, relative to "Activiti" framework.

Basically, I was forced to throw out jpa and jdbc plugins of Play!. Replacing their functionality by known open-source 3-d parties. It's OK. Play JPA plugin is too narrow-scoped for my case.

As result transaction managed by Spring transaction manager and entire example works with spring JPA annotations(using Hibernate JPA). It is really cross-platform example, because you can take all the spring stuff and move from play to other framework.

Good luck!

Vladimir Nabokov
  • 1,797
  • 1
  • 14
  • 19