[play 2.5 scala] ClassNotFoundException on model classes

44 views
Skip to first unread message

Edgars

unread,
May 27, 2016, 4:19:18 AM5/27/16
to play-framework
Hi,

is there a basepath or anything else necessary to create object using Class.forName?

Example:

abstract class SomeBaseController (...)
{
val SomeData = Class.forName("models.SomeDAO.SomeRepo").asInstanceOf[Class[_ <: models.SomeDAO.SomeRepo]].newInstance()
}

And I always get Error injecting constructor, java.lang.ClassNotFoundException: models.SomeDAO.SomeRepo

Will Sargent

unread,
May 27, 2016, 12:58:57 PM5/27/16
to play-fr...@googlegroups.com
It's best if you don't use Class.forName, as access to the base classloader is not needed for the system.  

The recommended practice for a "service" or "repository" class is to bind the class through Guice module, which you can do by adding an "app/Modules.scala" file into the application and then binding it as an eager singleton.  See the play-scala seed project for example:


From there, you can use @Inject() in the constructor to reference the instance.

Will.



--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/f1924b3e-c7a1-4cdc-8a78-939b5c529ce9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Edgars

unread,
May 30, 2016, 4:23:27 AM5/30/16
to play-framework
This works, and I can see point you are making, but I chose previous example because I need to pass different DAO.repo table classes for different controllers, that will extend this abstract BaseController. 
How can this be done with injection instead of passing string as class name?

Will Sargent

unread,
May 31, 2016, 9:53:31 AM5/31/16
to Edgars, play-fr...@googlegroups.com
You can subclass your table classes and rely on the different types for injection, or you can use @Named(“tablename”) if all the table classes have the same type.

Will.
--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages