Just thinking of implementing Guice in scala
Any sample code ?
Unless you specifically need Guice to add Scala to an existing Java-based project that uses framework, I'd advocate that you just use built-in language features instead.
Traits and implicits can give you all the Dependency Injection you'll need. You might also want to search online for the Cake pattern.
This post about DI in Scala will be able to help you to get going with Guice. See Using Google Guice topic.
Related
I am using Guice as my dependency injection framework. I'd like something that I can add that will make creating REST services easier.
I've had a look at guice-servlet and it works well for directing paths to HTTP servlets, but that's all it appears to do, I was expecting a JAX-RS like annotation syntax to work but it doesn't.
Having not actually used JAX-RS I've googled around and it appears that Jersey is the reference implementation for this but it looks like it uses its own dependency injection framework and doesn't work well with Guice. In addition it has 5+MB worth of dependencies which seems alot for what I am after.
Is Guice designed in such a way that it doesn't lend itself to JAX-RS, if so what else should I be doing?
I think that maybe the guice-servlet module has misguided you. Guice is a DI framework. Period. The real goal of the guice-servlet module is not providing the servlet's and filter's shortcut declaration but giving support for the special scopes request and session. Those nice shorcut declarations are syntatic sugar.
Chosing a JAX-RS implementation in Java is a bit out of the question. You have several options (Jersey, Resteasy, Spring...). If you are going full JavaEE then you don't have to choose. You just use the annotations (and the DI) out of the box.
If you are not using a JavaEE server (just a web server like Tomcat or any other fancy thing like an Android app) then you must choose your implementation. If you are using DI also (which I recommend) then there is one more decision to make.
So you are not using JavaEE and you want to implement some REST API using JAX-RS and dependency injection. You do some research and end up choosing Jersey and Guice. Good choice, I've chosen those in my last projects too. Yes, the dependency graph of Jersey is a bit bloated. I know, it could be way better.
Now your problem is how to make it work together because Jersey uses it's own DI framework (HK2) which is a bad thing.
You have a lot of references on SO about Jersey-Guice integration. Your best bet is the Guice HK2 bridge.
What? You want a direct reference on SO? No problem, here is a good one. Don't forget to upvote the answer. ;-)
Jersey and Google Guice integration
In my play 2.1 app I was using Guice, but to bind my interfaces to implementations for IoC I was using this library:
https://github.com/sptz45/sse-guice
Is this still needed for play 2.4? If not, do they have their own helpers that are like sse-guice?
As the What’s new in Play 2.4 page states:
Play now supports dependency injection out of the box.
You have the following options for DI in Play 2.4.x:
An implementation that uses Guice out of the box
An abstraction that allows other JSR 330 implementations to be plugged in
All Play components can be instantiated using plain constructors or factory methods
Traits that instantiate Play components that can be mixed together in a cake pattern like style to assist with compile time dependency injection
You can read more about Play’s dependency injection support for Scala.
is it possible to use JSR-303 for validation or especially validation groups in the scala version of play? I could not find anything in the documentation about this...
Best Regards!
I am deciding if I should learn Groovy and Scala, and currently I am comparing web frameworks. I like the Groovy language a bit more, but Play! works with both Java and Scala. Another thing pointing me towards Play! is the tight CoffeeScript Integration. For collaboration purposes, I need my framework to be able to affectively communicate and interact with some sort of JavaScript. For my choice I have a few questions on the Grails frameworks.
Questions
Can Grails use Java, or just Groovy?
Does Grails have any tight JavaScript integration? I mean can the two technologies communicate at runtime?
The Eclipse Grails Project seems to be maintained by spring. Does this mean Groovy can use Spring?
I am still confused on which to learn. Hopefully the answers to these questions will help me decide which to learn.
All of this information is widely available on the internet, easily found using Google or your favorite search engine. That said, here are the answers to your questions:
Question: Can Grails use Java, or just Groovy?
Answer: Grails uses both. Groovy is a dynamic language which runs within the JVM and in a lot of ways extends Java. Grails uses both Java and Groovy. You can use both within your Grails project as well. This allows you to leverage the vast array of Java libraries available. Much like Grails itself does.
Question: Does Grails have any tight JavaScript integration? I mean can the two technologies communicate at runtime?
Answer: Any Javascript library can be used, and they can (if you make them) communicate at runtime. (Your question doesn't really make a lot of sense). In regards to coffeescript (which you mentioned in your question), there are plugins available that will compile coffeescript at build time for your Grails project.
Question: The Eclipse Grails Project seems to be maintained by spring. Does this mean Groovy can use Spring?
Answer: Grails is built upon Spring, so yes. You (and it) can use Spring. There are a great number of reasons to leverage the Spring framework within your Grails project, and with the Bean builder DSL it's fairly straight forward to do so.
Matt Raible has a great comparison of all the JVM frameworks. I suggest checking out Play and Grails within that comparison to see how they rank in the broader comparison...
http://www.slideshare.net/mraible/comparing-jvm-web-frameworks-february-2014
You can very well use Java in Grails framework. There are two options:
1. Grails has a separate source folder for Java - here you can use Java classes
2. You can also use Java code inside a Groovy classes
Grails uses Spring and Hibernate
Try both out. Choosing a web framework is a big decision and it's worth spending some time choosing.
I wanted to know if it was possible to use Spring MVC with a different JVM language than Java, e.g. Groovy or Scala. Or can Groovy only be run on Grails?
Also if it is possible, is this something which people try often, or do they just stick to the framework traditionally used?
Spring works perfectly well with scala because scala compiles to normal .class files which are Java-equivalent bytecode. I use Spring and scala all the time. It's even possible to use the Spring XML-extensibility to add support for scala-specific types, for example:
<bean class="my.scala.Class">
<property name="listProp">
<scala:list value-type="java.lang.Integer">
<value>1</value>
</scala:list>
</property>
</bean>
Sure, spring has excellent support for dynamic languages like Groovy. There is an entire chapter in the reference manual: http://static.springsource.org/spring/docs/2.5.6/reference/dynamic-language.html
As for scala; I tried doing this and it is possible. The problem is that JSP (or for that matter most templating languages supported by spring mvc) doesn't 'understand' scala collection types so I found myself converting between scala and java collections quite a lot. This should be better in Scala 2.8.0 but I haven't tested this myself.
Grails is implemented with Spring baked in, Grails 3 uses Spring-Boot. So that is definitely possible. You could also use Scala with Spring MVC. I don't know that you'd get the most mileage out of Scala with Spring MVC -- not a lot of opportunities to use Scala's functional programming features -- but there's nothing stopping you from trying it out.
I cannot speak for Scala, but I have personally used Spring with Groovy. Match made in heaven :)
Spring works very well with Scala, although Scala itself has features that mean you don't need
a dependency injection framework (such as Spring) in many cases.
Also, Scala isn't a dynamic language, it's a static language just as Java is. One of the primary goals of Scala was maximum interoperability with Java. This means that Scala compiles down to class files that look and feel just like Java objects, and can be seamlessly used as Java objects by external libraries and frameworks, such as Spring.
Well, what about writing the business logic in scala, so that the functional way scales well in multic core environments and let the rest handled by Spring and Hibernate. I think this is a pretty nice way to get the best of both worlds. Of course one can usw lift as a web app framework, but i think Spring is more actually mature and has more features. But the business logic is where the ball is rolling and so Scala and FP can handle this better than imperative java.
Right?
Your question should be classified:
groovy or scala compile to class and deployed
groovy not compile just deployed to tomcat
using traditional spring framework with a little groovy
using brand new framework to work with groovy e.g. grail
If you want using traditional spring framework e.g. spingmvc springcloud and want to add some groovy to implement business logic code, please use a new lib micro-mvc: https://github.com/jeffreyning/micro-mvc
This lib can scan interface as springbean and create a proxy to execute groovy.