Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to effectively use Scala in a Spring MVC project?

I want to create a multi-module (maven) spring MVC application, with modules like:

web
persistance (hibernate)
core (general libs)
models
job-server (queue based jobs)
services (business logic)

Now to use Scala in this project, can I write scala everywhere without any problems? Or should I use scala for my services module, and java for the web module?

I know hibernate will have to be in java.

Thoughts? Advice?

Are there any issues to look out for? Is this a good idea? Will I have to hack certain parts to glue them together?

like image 593
loyalflow Avatar asked Jun 26 '12 20:06

loyalflow


People also ask

Can I use Scala with Spring?

Functional languages, such as Scala, encourage immutable data structures. As such, constructor injection is preferred when using Spring in Scala. Also note that constructor injection works out-of-the-box; you do not need the Spring Scala jar on the class path for it to work.

Is Spring MVC still used?

Yes. Whenever someone uses Spring Boot for creating RESTful web services, chances are they are using Spring MVC (part of Spring Web starter, see http://start.spring.io and search Web in dependencies) although Spring has also been offering another web framework, Spring WebFlux.

What is backend controller in Spring MVC?

controllers are responsible for receiving the request from the user and calling the back-end services. the figure below shows the flow of requests in the spring mvc framework. when a request is sent to the spring mvc framework the following sequence of events happen. the dispatcherservlet first receives the request.

What is ModelAndView in Spring MVC?

ModelAndView is a holder for both Model and View in the web MVC framework. These two classes are distinct; ModelAndView merely holds both to make it possible for a controller to return both model and view in a single return value. The view is resolved by a ViewResolver object; the model is data stored in a Map .


2 Answers

Yes, you can use Scala for both your Spring MVC code and your Hibernate classes.

Here is an example of a Spring MVC Controller in Scala taken from one of my projects:

@Controller
class HomeController {
  val log: Logger = LoggerFactory.getLogger(this.getClass.getName)

  @RequestMapping(Array("/"))
  def home: String = {
    log.debug("HomeController::home")

    "home/home"
  }
}

And an example of a Hibernate domain class. Note the use of private Java collection classes for Hibernate adapted by public Scala collection classes for users of the class (helped by Scala's JavaConversions class).

@Entity
class Role {
  @Id @GeneratedValue
  var id: Long = _

  @Index(name="role_name")
  var name: String = _

  var created_at: Date = _
  var updated_at: Date = _  

  @ManyToMany
  private var sub_role: java.util.Set[Role] = _
  def subRoles: Set[Role] = {
    if (sub_role == null) sub_role = new java.util.HashSet[Role]
    sub_role
  }

  @ManyToMany
  private var permission: java.util.Set[Permission] = _
  def permissions: Set[Permission] = {
    if (permission == null) permission = new java.util.HashSet[Permission]
    permission
  }
}

The only thing I've found that I've had to use Java for is writing @Retention(RUNTIME) annotations (for JSR-303 validations).

I use Maven and the maven-scala-plugin with mixed Java/Scala projects.

like image 144
sourcedelica Avatar answered Oct 20 '22 07:10

sourcedelica


You can use scala & java together for whatever you think each works best for.

I know hibernate will have to be in java.

This isn't necessarily true. You can use scala classes as Hibernate ORM objects. You just have to make sure that the compiled .class files end up with the same getters, setters, and default constructor that Hibernate expects.

Even your Web App module can be written in scala. The controllers just need to abide by the contract which Spring MVC expects.

In general, almost anything written in Java can be re-written in scala. Of coarse there are exceptions but don't per-maturaly limit yourself from trying. Assuming your existing code dones't do anything to crazy (i.e. black-magic reflection), it should be fine.

The interesting question here is how to get it working with Maven...

There is an existing SO question which describes using the maven-scala-plugin to get this kind of project working (multi-module with mixed scala and java resources). The only extra comment I will add to this solution is that, in my experience, we could only get it working with scala modules being dependent on java sources, not the other way around. This was because our maven config used the java compiler and scala compiler seprarately. In theory, the scala compiler can compile Java files too so if you configured it to just use one compiler for everything, you wouldn't encounter this limitation.

EDIT:

Blankman commented:
@Jesse so you have done this also then? not sure I understood the maven issue.

Yes, I have worked on a couple projects which have used mixed java and scala sources. We didn't use Scala for the Hibernate layer but we did for everything else.

The maven issue I am describing was a consequence of our maven-scala-plugin configuration. You have to configure it to know how to compile your scala files. Our configuration (probably not correct) was setup to compile java files first, using javac, and then compile scala files, using scalac. This meant that we weren't allowed to write java code which referenced scala classes because they wouldn't not be available at compile time. With the proper configuration, it should be possible to use the scalac compiler to compile all your sources together which will remove this awkward restriction. The SO question I linked to above has example configuration as well as some discussion about the compile time issues I am talking about. If you need more reference for setting up the plugin, check out the plugin's website (also linked to above) for the official doc.

like image 3
Jesse Webb Avatar answered Oct 20 '22 08:10

Jesse Webb